I am trying to create a User Form that will enter data into a spreadsheet. I want the user of the form to be able to enter a date as the first piece of information in the User Form, this date will then correspond with the row in which the remaining data is entered (note all relevant dates are already in the spreadsheet so I am just doing a find function to locate the date in the row corresponding to where I want the data to go)
I am very new to VBA so I am not sure that I am even approaching this the right way but here is what I have so far:
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sales")
'find matching data row in database
iRow = Cells.Find(What:=Me.txtDate.Value, After:=ws.Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
'check for a date
If Trim(Me.txtDate.Value) = "" Then
Me.txtDate.SetFocus
MsgBox "Please enter a date"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 5).Value = Me.txtSmall.Value
ws.Cells(iRow, 6).Value = Me.txtMedium.Value
ws.Cells(iRow, 7).Value = Me.txtLarge.Value
ws.Cells(iRow, 8).Value = Me.txtXL.Value
'clear the data
Me.txtSmall.Value = ""
Me.txtMedium.Value = ""
Me.txtLarge.Value = ""
Me.txtXL.Value = ""
Me.txtDate.SetFocus
End Sub
I'm not sure why this isn't working, I am getting an error message:
Run-time error '1004':
Application-defined or object-defined error
Not sure what it means but I did notice when I run the cursor over iRow in the ws.cells(iRow, 5) it is displaying a value of -1 which seems wrong to me.
Any help would be greatly appreciated, thanks!
Bookmarks