简体   繁体   English

Excel Userform VBA不会移到下一行

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

My userform works fine to fill in the fields on an excel spreadsheet, but it refuses to move to the next line. 我的用户表单可以很好地填写excel电子表格中的字段,但拒绝移至下一行。 I can't do it by asking to find an empty row either, because columns A and C are prefilled. 我也无法通过请求查找空行来完成此操作,因为列A和C已预先填充。 Is there a way to make the userform jump to the next line once it has been complete and submitted and the fields are filled out, maybe by using the B column as a way to know whether the row is open to use? 有没有一种方法可以使用户窗体在完成并提交并填写字段后跳到下一行,也许是通过使用B列作为了解该行是否开放使用的方式? Because no matter what, the A and C columns are filled. 因为无论如何,将填充A和C列。 Also, once the user is done, saves the document, and comes back to it later, will the userform pick up on whatever line it last left off on? 而且,一旦用户完成操作,保存文档,然后再返回文档,用户表单会在上一次停止的行上显示吗? I know i posted this already, but no solutions have worked and I figured I would add more detail. 我知道我已经发布了此信息,但是没有解决方案起作用,我想我会添加更多细节。 Sorry if some of this seems novice, I am new to VBA. 抱歉,如果其中有些似乎是新手,我是VBA的新手。 Thank you! 谢谢!

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

The code below will find the last used row in EXPECTED RETURNS sheet and copy data in next row. 下面的代码将在EXPECTED RETURNS表中找到最后使用的行,并在下一行中复制数据。 Also, it assumes that you set a tag number to all your relevant elements in the form: 1 for TextDate , 3 for TextDevice and so on. 同样,它假定您以以下形式为所有相关元素设置tag号:1表示TextDate ,3表示TextDevice ,依此类推。 This way, your code can be reduce to something like the following: 这样,您的代码可以简化为以下形式:

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

Try this instead... 试试这个...

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"

You can add na iff statement to have a positive result and a negative result if you want 您可以添加na iff语句,以得到肯定的结果和否定的结果

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

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