Search code examples
excelvbarangerowuserform

Click submit in userform to fill data in next row


I created a userform.

If I click submit I get next cell selected. When I run the userform again data are pasted into the same cells like before.

This is VBA part of the "submit" command on the userform:

Private Sub CommandButton2_Click()
    Sheets("Test").Range("A2").value = TextBox3.value
    Sheets("Test").Range("B2").value = TextBox4.Text
    Sheets("Test").Range("C2").value = TextBox5.Text
    Sheets("Test").Range("D2").value = TextBox6.Text
    Sheets("Test").Range("E2").value = ComboBox1.Text
    
    Unload Me
End Sub

I need when I press submit to go to next row and paste all data from userform.


Solution

  • Try this code, please. In the way you presented your data, it looks that the columns (from A to E) have the same number of rows. If not, you must calculate lastRow before filling data in each column:

    Private Sub CommandButton2_Click()
      Dim sh As workseet, lastRow As Long
      Set sh = Sheets("Test") 'it would be better to fully define it (workbook included)
      lastRow = sh.Range("A" & Rows.count).End(xlUp).Row + 1
        sh.Range("A" & lastRow).value = TextBox3.value
        sh.Range("B" & lastRow).value = TextBox4.text
        sh.Range("C" & lastRow).value = TextBox5.text
        sh.Range("D" & lastRow).value = TextBox6.text
        sh.Range("E" & lastRow).value = ComboBox1.value
    
        Unload Me
    End Sub