簡體   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