I apologize if this has already been answered or in the wrong section. I have looked for the answer in many places on the internet, including this website, and haven't found my answer.
The intent of the macro is to create a form that will enter in Item details and the state it will be shipped to. Categorizing the items by the state they are being shipped to. All states except Hawaii and Alaska.
I have a UserForm that collects data using textboxes. Textboxes 1-5 corresponding with the next empty cell and columns A - E.
TextBox4 is the state acronym. Based on the state entered, I would like it to select the corresponding sheet, then enter the data into the next empty row on that sheet.
example: TextBox4 = AL. I want the sheet associated with Alabama to be selected then if row 2 is empty, textboxes 1, 2, 3, and 5 will be inserted into the cells.
My Problem with the code is selecting the correct sheet (all renamed to the state they correspond with) and then selecting the correct cells. Any suggestions??
I know that I need a cell address in the "If block" in my code but I'm not sure how to select the last row. I attempted to use this:
Here is my code:
Thank in advance for your help!
The intent of the macro is to create a form that will enter in Item details and the state it will be shipped to. Categorizing the items by the state they are being shipped to. All states except Hawaii and Alaska.
I have a UserForm that collects data using textboxes. Textboxes 1-5 corresponding with the next empty cell and columns A - E.
TextBox4 is the state acronym. Based on the state entered, I would like it to select the corresponding sheet, then enter the data into the next empty row on that sheet.
example: TextBox4 = AL. I want the sheet associated with Alabama to be selected then if row 2 is empty, textboxes 1, 2, 3, and 5 will be inserted into the cells.
My Problem with the code is selecting the correct sheet (all renamed to the state they correspond with) and then selecting the correct cells. Any suggestions??
I know that I need a cell address in the "If block" in my code but I'm not sure how to select the last row. I attempted to use this:
Code:
[COLOR=#00356A][FONT=Arial]LastRow = Range("A65536").End(xlUp).Row[/FONT][/COLOR]
Here is my code:
Code:
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim LastRow As Object
Dim ws As Worksheet
[COLOR=#00356A][FONT=Arial]LastRow = Range("A65536").End(xlUp).Row[/FONT][/COLOR]
If Me.TextBox1 = "" Or Me.TextBox2 = "" _
Or Me.TextBox3 = "" Or Me.TextBox4 = "" _
Then MsgBox ("All Fields Must be Completed")
If TextBox4.Value = "AL" Then
Sheets("AL").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "AZ" Then
Sheets("AZ").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "AR" Then
Sheets("AR").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "CA" Then
Sheets("CA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "CO" Then
Sheets("CO").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "CT" Then
Sheets("CT").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "DE" Then
Sheets("DE").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "DC" Then
Sheets("DC").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "FL" Then
Sheets("FL").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "GA" Then
Sheets("GA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "ID" Then
Sheets("ID").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "IN" Then
Sheets("IN").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "IA" Then
Sheets("IA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "KS" Then
Sheets("KS").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "KY" Then
Sheets("KY").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "LA" Then
Sheets("LA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "ME" Then
Sheets("ME").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "MD" Then
Sheets("MD").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "MA" Then
Sheets("MA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "MI" Then
Sheets("ME").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "MN" Then
Sheets("MN").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "MS" Then
Sheets("MS").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "MO" Then
Sheets("MO").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "MT" Then
Sheets("MT").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "NE" Then
Sheets("NE").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "NV" Then
Sheets("NV").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "NH" Then
Sheets("NH").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "NJ" Then
Sheets("NJ").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "NM" Then
Sheets("NM").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "NY" Then
Sheets("NY").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "NC" Then
Sheets("NC").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "ND" Then
Sheets("ND").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "OH" Then
Sheets("OH").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "OK" Then
Sheets("OK").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "OR" Then
Sheets("OR").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "PA" Then
Sheets("PA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "RI" Then
Sheets("RI").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "SC" Then
Sheets("SC").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "SD" Then
Sheets("SD").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "TN" Then
Sheets("TN").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "TX" Then
Sheets("TX").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "UT" Then
Sheets("UT").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "VT" Then
Sheets("VT").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "VA" Then
Sheets("VA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "WA" Then
Sheets("WA").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "WV" Then
Sheets("WV").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "WI" Then
Sheets("WI").Cells().Value = TextBox1.Text
ElseIf TextBox4.Value = "WY" Then
Sheets("WY").Cells().Value = TextBox1.Text
Else: MsgBox "Enter a valid state"
End If
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
With ws
LastRow.Offset(iRow, 1).Value = TextBox1.Value
LastRow.Offset(iRow, 2).Value = TextBox2.Value
LastRow.Offset(iRow, 3).Value = TextBox3.Value
LastRow.Offset(iRow, 4).Value = TextBox4.Value
LastRow.Offset(iRow, 5).Value = TextBox5.Value
End With
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
End Sub
Thank in advance for your help!