I'm trying to create a UserForm which will open when I select a command button. The purpose of it is to capture data from a selected row concerning a "tour" - i.e. the tour code, start date and end date, and then for me to "split" the tour, for which I need to enter new tour codes, start dates and end dates.
I need the data from the form to go into a separate sheet (called "splits"), so that I have a record of the original tour details, and the new tour details in one sheet. But I get a Run-Time error '1004' (Application-defined or object-defined error) when I try to run the macro. I'm new to VBA and I don't know what I've done wrong!
This is my code so far:
Private Sub UserForm_Initialize()
With Me
.OriginalTourCode.Value = Cells(ActiveCell.Row, "A").Value
.OriginalStartDate.Value = Cells(ActiveCell.Row, "B").Value
.OriginalEndDate.Value = Cells(ActiveCell.Row, "C").Value
End With
End Sub
Private Sub SplitTourCommand_Click()
Dim ctrl As Control
Dim ws As Worksheet
Set ws = Sheets("Splits")
erow = ws.Cells(Rows.Count, 1).End(x1Up).Offset(1, 0)
Cells(erow, 1) = OriginalTourCode.Text
Cells(erow, 2) = OriginalStartDate.Text
Cells(erow, 3) = OriginalEndDate.Text
Cells(erow, 4) = NewTourCode1.Text
Cells(erow, 5) = NewStartDate1.Text
Cells(erow, 6) = NewEndDate1.Text
Cells(erow, 7) = NewTourCode2.Text
Cells(erow, 8) = NewStartDate2.Text
Cells(erow, 9) = NewEndDate2.Text
Cells(erow, 10) = ReasonForSplit.Text
End Sub
Private Sub CloseCommand_Click()
Unload Me
End Sub
The Userform Intitialise section automatically fills in the first three cells of the UserForm, and then I'll use the form to enter the new data.
The Close command section is just a separate button on the form to exit out.
How to I get the form, when I click the "split tour" command button, to enter the data into the next empty row of the "splits" sheet?
Thanks so much in advance for helping.
erow
contain? (And what data type has it been defined as?) You can useDebug.Print
to output it to the Immediate Window - ChronocidalDebug.Print erow
after theerow = ..
row, then it will output the value for you to look at - but, you would actually get an error before that (see my answer below) - Chronocidal