簡體   English   中英

Excel Userform VBA不會移到下一行

[英]Excel Userform VBA won't move to next row

我的用戶表單可以很好地填寫excel電子表格中的字段,但拒絕移至下一行。 我也無法通過請求查找空行來完成此操作,因為列A和C已預先填充。 有沒有一種方法可以使用戶窗體在完成並提交並填寫字段后跳到下一行,也許是通過使用B列作為了解該行是否開放使用的方式? 因為無論如何,將填充A和C列。 而且,一旦用戶完成操作,保存文檔,然后再返回文檔,用戶表單會在上一次停止的行上顯示嗎? 我知道我已經發布了此信息,但是沒有解決方案起作用,我想我會添加更多細節。 抱歉,如果其中有些似乎是新手,我是VBA的新手。 謝謝!

Private Sub butOK_Click()
Dim RowCount As Long
Dim ctl As Control

RowCount = Worksheets("EXPECTED RETURNS").Range("A6865").CurrentRegion.Rows.Count
 With Worksheets("EXPECTED RETURNS").Range("A6865")
.Offset(LastRow, 1).Value = Me.txtDate.Value
.Offset(LastRow, 3).Value = Me.txtDevice.Value
.Offset(LastRow, 4).Value = Me.txtID.Value
.Offset(LastRow, 5).Value = Me.txtSN.Value
.Offset(LastRow, 6).Value = Me.txtTrans.Value
.Offset(LastRow, 7).Value = Me.txtIDTrans.Value
.Offset(LastRow, 8).Value = Me.txtMS.Value
.Offset(LastRow, 9).Value = Me.txtCountry.Value
.Offset(LastRow, 10).Value = Me.txtCamp.Value
.Offset(LastRow, 11).Value = Me.txtOrig.Value
.Offset(LastRow, 12).Value = Me.txtProgram.Value
.Offset(LastRow, 13).Value = Me.txtPOC.Value
.Offset(LastRow, 14).Value = Me.txtPOCEmail.Value
.Offset(LastRow, 15).Value = Me.txtDSN.Value
.Offset(LastRow, 16).Value = Me.txtIR.Value
.Offset(LastRow, 17).Value = Me.txtEI.Value
End With

For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
ctl.Value = ""
ElseIf TypeName(ctl) = "CheckBox" Then
ctl.Value = False
End If
Next ctl

End Sub

下面的代碼將在EXPECTED RETURNS表中找到最后使用的行,並在下一行中復制數據。 同樣,它假定您以以下形式為所有相關元素設置tag號:1表示TextDate ,3表示TextDevice ,依此類推。 這樣,您的代碼可以簡化為以下形式:

Private Sub butOK_Click()
    Dim LastRow As Long
    Dim ctl As Control
    Dim i As Integer

    'Get last row with data
    LastRow = Worksheets("EXPECTED RETURNS").Range("A" & Rows.Count).End(xlUp).Row + 1

    'Get all control values, and write to column according to their tag
    For Each ctl In Me.Controls
        If ctl.Tag <> "" Then
            i = ctl.Tag
            Worksheets("EXPECTED RETURNS").Cells(LastRow, i) = ctl.Value
        End If
    Next

    'Clear all controls
    For Each ctl In Me.Controls
        If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
            ctl.Value = ""
        ElseIf TypeName(ctl) = "CheckBox" Then
            ctl.Value = False
        End If
    Next ctl
End Sub

試試這個...

last = Application.ThisWorkbook.Worksheets("YOURSHEET").Range("A65536").End(xlUp).Row

    Application.ThisWorkbook.Worksheets("Yourshee").Cells(last + 1, 1) = anything()                          'data
    Application.ThisWorkbook.Worksheets("Yoursheet").Cells(last + 1, 2) = anything            
    Application.ThisWorkbook.Worksheets("Yoursheet").Cells(last + 1, 3) = anything
    Application.ThisWorkbook.Worksheets("Yoursheet").Cells(last + 1, 4) = anything            
    Application.ThisWorkbook.Worksheets("Yoursheet").Cells(last + 1, 5) = anything 
    MsgBox "Sucess"

您可以添加na iff語句,以得到肯定的結果和否定的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM