1

I am pretty new to VBA, I have a userform created and all I want it to do is after the enter button is clicked, it adds the filled-in text boxes onto the next empty row in the excel sheet. I get object variable not set (on the rw = ws.Cells line) and I'm not sure how to set it.

Thanks so much.

Private Sub enter_Click()
Dim rw As Integer
Dim ws As Worksheet
Set ws = Worksheets("Home")
rw = ws.Cells.Find(What:=” * ”, SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws.Cells(rw, 1).Value = Me.TextBox1.Value
ws.Cells(rw, 2).Value = Me.TextBox2.Value
End Sub

1 Answer 1

2

Try this.

You need straight quotes rather than those curly ones.

When using Find always build in a check that your term is found otherwise you will get an error when trying to assign a row to Nothing.

Also set the After parameter if you are looking for the first unused row - start at the top and work up, i.e. loop back round to the bottom and up.

Private Sub enter_Click()

Dim rw As Long  'rather than integer
Dim ws As Worksheet
Dim r As Range

Set ws = Worksheets("Home")
Set r = ws.Cells.Find(What:="*", after:=ws.Cells(1), SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues)
'alternatively could use rw=ws.range("A" & rows.count).end(xlup).row + 1

If Not r Is Nothing Then
    ws.Cells(r.row+1, 1).Value = Me.TextBox1.Value
    ws.Cells(r.Row + 1, 2).Value = Me.TextBox2.Value
End If

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.