vba excel用户窗体移动到下一个空行

vba excel用户窗体移动到下一个空行,excel,vba,Excel,Vba,我最近创建了一个带有各种ActiveX控件的VBA用户窗体,但遇到以下问题: 将数据从userform保存到工作表 将数据输入工作表中的下一个可用行(创建多个记录) 重置新数据输入的用户表单 我有一个命令按钮,它使用(未成功)以下代码: Private Sub cmdSubmit_Click() Dim ws As Worksheet Dim addme As Range Set ws = Sheet1 Set addme = ws.Cells(Rows.Coun

我最近创建了一个带有各种ActiveX控件的VBA用户窗体,但遇到以下问题:

  • 将数据从userform保存到工作表
  • 将数据输入工作表中的下一个可用行(创建多个记录)
  • 重置新数据输入的用户表单
  • 我有一个命令按钮,它使用(未成功)以下代码:

    Private Sub cmdSubmit_Click()
        Dim ws As Worksheet
        Dim addme As Range
        Set ws = Sheet1
        Set addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    
        With ws
            addme.Offset.Value = Me.txtNeedsAnalysSum
            addme.Offset.Value = Me.txtSummaryOfTask
            addme.Offset.Value = Me.txtIntroduction
            addme.Offset.Value = Me.chkInRes
            addme.Offset.Value = Me.chkOnline
            addme.Offset.Value = Me.chk24Hr
            addme.Offset.Value = Me.chk3days
            addme.Offset.Value = Me.chkDurOther
            addme.Offset.Value = Me.cmbPrereqReq
            addme.Offset.Value = Me.cmbPrereqRec
        End With
    End Sub
    
    感谢您的帮助


    -乔

    我想你应该做一些事情,比如:

    Private Sub cmdSubmit_Click()
        Dim ws As Worksheet
        Dim addme As Range
        Set ws = Sheet1
        Set addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    
    
            addme.Offset(,1).Value = Me.txtNeedsAnalysSum
            addme.Offset(,2).Value = Me.txtSummaryOfTask
            addme.Offset(,3).Value = Me.txtIntroduction
            addme.Offset(,4).Value = Me.chkInRes
            addme.Offset(,5).Value = Me.chkOnline
            addme.Offset(,6).Value = Me.chk24Hr
            addme.Offset(,7).Value = Me.chk3days
            addme.Offset(,8).Value = Me.chkDurOther
            addme.Offset(,9).Value = Me.cmbPrereqReq
            addme.Offset(,10).Value = Me.cmbPrereqRec
    
    End Sub
    
    您可以通过表单中的控件进行循环,并使用变量跟踪要写入的列:

    Private Sub cmdSubmit_Click()
        Dim ws As Worksheet
        Dim addme As Range
        Set ws = Sheet1
        Set addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    
        Dim cntrl As control
        Dim intCol as integer
        intCol = 0
        For Each cntrl in Me.Controls
            addme.offset(, intCol) = cntrl
            intCol = intCol + 1
        Next cntrl        
    End Sub
    

    这也将拾取标签和提交按钮以及您拥有的内容,因此,YMMV。

    类似于以下内容的操作应该可以完成:

    Private Sub cmdSubmit_Click()
        Dim ws As Worksheet
        Dim addme As Long
        Set ws = Sheet1
        addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
        With ws
            ws.Cells(addme, 1).Value = Me.txtNeedsAnalysSum 'the number 1 here represents the Column A
            ws.Cells(addme, 2).Value = Me.txtSummaryOfTask 'the number 2 represents Column B
            ws.Cells(addme, 3).Value = Me.txtIntroduction
            ws.Cells(addme, 4).Value = Me.chkInRes
            ws.Cells(addme, 5).Value = Me.chkOnline
            ws.Cells(addme, 6).Value = Me.chk24Hr
            ws.Cells(addme, 7).Value = Me.chk3days
            ws.Cells(addme, 8).Value = Me.chkDurOther
            ws.Cells(addme, 9).Value = Me.cmbPrereqReq
            ws.Cells(addme, 10).Value = Me.cmbPrereqRec
        End With
            Me.txtNeedsAnalysSum = vbNullString 're-set your textboxes
            Me.txtSummaryOfTask = vbNullString
            Me.txtIntroduction = vbNullString
            Me.chkInRes = False
            Me.chkOnline = False
            Me.chk24Hr = False
            Me.chk3days = False
            Me.chkDurOther = False
            Me.cmbPrereqReq = ""
            Me.cmbPrereqRec = ""
    End Sub
    

    还没有深入研究,但是你的
    和ws
    是多余的。范围对象
    addme
    知道它所在的工作表。在
    addme.Offset.Value
    中,需要指定偏移量,如
    addme.Offset(0,1).Value
    。之后,重置文本框将像
    Me.txtneedsanalysissum=vbNullString
    一样简单,对于复选框
    Me.chkInRes=False
    。有关重置组合框的信息,请参阅文本框。谢谢!最后一个代码(Xabier)运行得非常好。我学到了一些东西。非常感谢。如果我的回答对您有所帮助,请您将其标记为答案?谢谢