繁体   English   中英

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

[英]vba excel userform move to next empty row

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

  1. 将数据从用户表单保存到工作表
  2. 将数据输入到工作表中的下一个可用行(创建多个记录)
  3. 重置用户表单以输入新数据

我有一个使用(未成功)以下代码的命令按钮:

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

我认为您想做类似的事情:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM