简体   繁体   中英

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. I can't do it by asking to find an empty row either, because columns A and C are prefilled. 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? Because no matter what, the A and C columns are filled. 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. 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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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