vba excel userform移动到下一个空行

时间:2018-01-17 15:56:54

标签: excel-vba vba excel

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

  1. 将数据从用户表单保存到工作表
  2. 将数据输入工作表中的下一个可用行(创建多个记录)
  3. 重置用户表单以获取新数据条目
  4. 我有一个使用(失败)以下代码的命令按钮:

    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
    

    感谢任何帮助!

    -Joe

2 个答案:

答案 0 :(得分:1)

我想你想做点什么:

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。

答案 1 :(得分:0)

以下内容应该这样做:

ArrayLists