【问题标题】:Inserting values from a UserForm to excel sheet which start from a fixed cell?将用户窗体中的值插入从固定单元格开始的 excel 工作表?
【发布时间】:2021-04-16 14:25:11
【问题描述】:

我有一个单元格范围 Sheets("INVOICE MAKER").Range("D18:D37")(总共 20 个单元格),还有一个名为 Add Items 的小 UserForm

UserForm中有一个Textbox和一个Submit Button

因此,如果我在该 文本框 中写入内容并单击 提交按钮,则应将数据写入范围 Sheets("INVOICE MAKER").Range("D18:D37") 中的下一个可用空单元格。如果所有 20 个单元格都填充了数据,则显示类似 “没有更多行可用于写入数据”的消息

下面的代码不是从Cell D18开始写入数据,而是从D1开始写入数据。 并且在单元格 D37 之后不会停止。

Option Explicit
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("INVOICE MAKER")

lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    
'check for a part number
If Trim(Me.Textbox.Value) = "" Then
  Me.Textbox.SetFocus
  MsgBox "Please Type Item Name"
  Exit Sub
End If

With ws
  .Cells(lRow, 5).Value = Me.Textbox.Value
End With

End Sub

【问题讨论】:

    标签: vba


    【解决方案1】:

    希望以下代码对您有所帮助:

    Public Working_Sheet As Worksheet
    Public All_Cell_Value As Boolean
    Public Write_Cell_No As Integer
    Public Content As String
    
    'When button in the form is clicked
    Sub Button1_Click()
    Write_Content
    End Sub
    
    'validation and content writing function
    Public Function Write_Content()
    All_Cell_Value = True
    Set Working_Sheet = Worksheets("Sheet1")
    
    For i = 18 To 37
    
    If Trim(Working_Sheet.Cells(i, "D")) = "" Then
    All_Cell_Value = False
    Write_Cell_No = i
    Exit For
    End If
    
    Next i
    
    If All_Cell_Value = False Then
    Content = InputBox("Enter the value")
    If Content = "" Then
    MsgBox ("No Data")
    Else
    Working_Sheet.Cells(i, "D").Value = Content
    End If
    
    Else
    MsgBox ("Sorry content is full")
    End If
    
    
    End Function
    

    【讨论】:

    • 谢谢@na4su 先生和先生。埃尔伯特比利亚雷亚尔。两个答案都很好。但是 Stack Overflow 不允许同时接受这两个答案。
    【解决方案2】:

    也许这会有所帮助!

    编辑 #1 修复一些错误,代码必须在Form内,TextBoxButton

    编辑 #2userform 或宏添加了结束语句

    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim lRow As Long
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Worksheets("INVOICE MAKER") 'Set from Thisworkbook
        ws.Activate 'activate the Invoice Maker
        'lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
        Dim iniCell As Integer: iniCell = 18
        Dim endCell As Integer: endCell = 37
        Dim setCol As Integer: setCol = ws.Range("D1").Column
        'As you said, the range is D18:D37, then you can manipulate from this 3 vars
        Dim iRange As Range
        Dim i As Range
        Dim isTheRangeFull As Boolean: isTheRangeFull = False
        Dim iMsgbox As Integer
    
        If Trim(Me.TextBox.Value) = "" Then
            Me.TextBox.SetFocus
            'Ask the user to retry or quit.
            iMsgbox = MsgBox("Please Type Item Name" & Chr(10) & "Do you want to retry?", vbYesNo + vbDefaultButton1)
            If iMsgbox = 6 Then
                GoTo InsertData
                'if the user say YES
                'do it again
            ElseIf iMsgbox = 7 Then
                End
                'if the user say NO
            End If
        End If
        Set iRange = ws.Range(Cells(iniCell, setCol), Cells(endCell, setCol))
        'set your working Range
        
        'This Loop do the job!
        For Each i In iRange
            isTheRangeFull = True
            'if there is no empty cell, won't enter the if, and
            'the var continue TRUE, so there is no empty cells...
            If i.Value = Empty Then
                i.Value = Me.TextBox.Value
                isTheRangeFull = False
                    'the Next line (End) Will close the Form and terminate the macro
                'End
                    'The next line just close the userform
                'Unload Me
                    'Decide which one to uncomment.
                Exit For
            End If
        Next i
        
        If isTheRangeFull Then
            MsgBox "No more rows are available to write data"
            End
        End If
        'With ws
        '  .Cells(lRow, 5).Value = Me.TextBox.Value
        'End With
    InsertData:
    End Sub
    

    【讨论】:

    • 感谢@ElbertVilliarreal 先生的热情回复。对于这一行 Dim setCol As Integer: Range("D1").Column,VBA 说 “编译错误:属性使用无效”
    • 谢谢@ElbertVillarreal 先生和先生。 na4su。两个答案都很好。但是 Stack Overflow 不允许同时接受这两个答案。
    猜你喜欢
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多