Not sure how to write the code for the command button when making a userform

andzia

New Member
Joined
Apr 11, 2015
Messages
1
Could someone kindly just look at the code and tell me what to change, Thank you
smile.gif

this is my first time doing this and I got really confused when looking at other people examples. Underneath is a screenshot of what I'm trying to create but every time I try to add the information this error pops up: "Compile error: Method or data member not found"

And this is the code I typed in:

Private Sub commandbutton_add_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")


'check for a Name number
If Trim(Me.textbox_name.Value) = "" Then
Me.textbox_name.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If


'copy the data to the database
ws.Cells(iRow, 1).Value = Me.textbox_title.Value
ws.Cells(iRow, 2).Value = Me.textbox_name.Value
ws.Cells(iRow, 3).Value = Me.textbox_surname.Value
ws.Cells(iRow, 4).Value = Me.textbox_address.Value
ws.Cells(iRow, 5).Value = Me.textbox_party.Value
ws.Cells(iRow, 6).Value = Me.TextBox_number.Value
ws.Cells(iRow, 7).Value = Me.textbox_date.Value


MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.textbox_title.Value = ""
Me.textbox_name.Value = ""
Me.textbox_surname.Value = ""
Me.textbox_address.Value = ""
Me.textbox_party.Value = ""
Me.TextBox_number.Value = ""
Me.textbox_date.Value = ""
Me.textbox_title.SetFocus


End Sub


 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
shouldn't this line be
Code:
If Trim(Me.textbox_name.Value) = "" Then

Code:
If Me.textbox_name.Value = "" Then

And If you use F8 to manually step through the code, it should highlight the error line in yellow
 
Upvote 0
iRow is the row number you are pasting the values to. You have not defined a value for iRow. By default, it's value is zero and there is no row zero.


This will set iRow as the next empty row on the worksheet though I don't know if that's what you want.

iRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top