I am very new to VBA, and have created a workbook with a number of sheets and tables that need to be populated with a userform. I have made my userform to have pages that match my workbook - i.e. Cartons Counts, Aisle Hours etc. as these need to be kept separate and are not all needed to be updated at the same time. I chose to add a new command button to each page in the userform that should populate the table on the corresponding sheet in my workbook when clicked. So far this works for the first three worksheets in the workbook from the pages in the userform, but the exact same code causes errors on the last 3 and it doesn't populate the associated tables at all. Every worksheet/table is set up the same way, as are the userform components and they all share a common set of data that is outside of the "page" in the userform (which works on the first 3, but not the last 3). I have checked all of the references are to the correct locations (i.e. the right worksheets and the right textboxes on the user form etc.) and cannot work out why the code is ONLY working for 3 sheets.
I have no idea what is going wrong as everything else seems to work ok in the other parts of the code. There is no special formulas or coding to any of the tables other than what is used when making the table, and renaming them according to the page for identification.
I have included the full code I have so far, and made the sections that are causing trouble bold. They all come up with the following error: "run-time error '91': Object variable or With block variable not set". The first sections of the code, for Transfer Buttons 1-3, the same code all works perfectly fine and the right tables on the right sheets are being populated with the right data from the userform.
Attached also are screenshots (not in XL2BB) of the excel workbook (from a dummy version) and of the userform to give an idea of what I mean by the userform pages corresponding to the workbook pages.
Any help here would be greatly appreciated.
I have no idea what is going wrong as everything else seems to work ok in the other parts of the code. There is no special formulas or coding to any of the tables other than what is used when making the table, and renaming them according to the page for identification.
I have included the full code I have so far, and made the sections that are causing trouble bold. They all come up with the following error: "run-time error '91': Object variable or With block variable not set". The first sections of the code, for Transfer Buttons 1-3, the same code all works perfectly fine and the right tables on the right sheets are being populated with the right data from the userform.
Attached also are screenshots (not in XL2BB) of the excel workbook (from a dummy version) and of the userform to give an idea of what I mean by the userform pages corresponding to the workbook pages.
Any help here would be greatly appreciated.
Rich (BB code):
Private Sub UserForm_Initialize()
DateBox1.SetFocus
LoadTypeCombo.List = Array("Single Sydney", "Single Melbourne", "Double Syd/Melb", "Double Sydney", "Double Melbourne", "Triple", "No NDC", "Other")
DayCombo.List = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
ComboBox1.List = Array("Yes", "No", "Partial")
ComboBox2.List = Array("Yes", "No", "Partial")
ComboBox3.List = Array("Yes", "No", "Partial")
ComboBox4.List = Array("Yes", "No", "Partial")
ComboBox5.List = Array("Yes", "No", "Partial")
ComboBox6.List = Array("Yes", "No", "Partial")
ComboBox7.List = Array("Yes", "No", "Partial")
ComboBox8.List = Array("Yes", "No", "Partial")
ComboBox9.List = Array("Yes", "No", "Partial")
ComboBox10.List = Array("Yes", "No", "Partial")
ComboBox11.List = Array("Yes", "No", "Partial")
ComboBox12.List = Array("Yes", "No", "Partial")
ComboBox13.List = Array("Yes", "No", "Partial")
ComboBox14.List = Array("Yes", "No", "Partial")
ComboBox15.List = Array("Yes", "No", "Partial")
End Sub
Private Sub Transfer1_Click()
Dim myWorksheet As Worksheet
Set myWorksheet = Worksheets("Carton Count")
Dim rng As Range
Set rng = myWorksheet.ListObjects("CartonCount").Range
Dim LastRow As Long
LastRow = rng.Find(What:=” * ”, _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = DateBox1.Text
rng.Parent.Cells(LastRow + 1, 2).Value = DayCombo.Text
rng.Parent.Cells(LastRow + 1, 3).Value = LoadTypeCombo.Text
rng.Parent.Cells(LastRow + 1, 4).Value = TextBox47.Text
rng.Parent.Cells(LastRow + 1, 5).Value = TextBox48.Text
rng.Parent.Cells(LastRow + 1, 6).Value = TextBox49.Text
rng.Parent.Cells(LastRow + 1, 7).Value = TextBox50.Text
rng.Parent.Cells(LastRow + 1, 8).Value = TextBox51.Text
rng.Parent.Cells(LastRow + 1, 9).Value = TextBox52.Text
rng.Parent.Cells(LastRow + 1, 10).Value = TextBox53.Text
rng.Parent.Cells(LastRow + 1, 11).Value = TextBox54.Text
rng.Parent.Cells(LastRow + 1, 12).Value = TextBox55.Text
rng.Parent.Cells(LastRow + 1, 13).Value = TextBox56.Text
rng.Parent.Cells(LastRow + 1, 14).Value = TextBox57.Text
rng.Parent.Cells(LastRow + 1, 15).Value = TextBox58.Text
rng.Parent.Cells(LastRow + 1, 16).Value = TextBox59.Text
rng.Parent.Cells(LastRow + 1, 17).Value = TextBox60.Text
rng.Parent.Cells(LastRow + 1, 18).Value = TextBox61.Text
End Sub
Private Sub Transfer2_Click()
Dim myWorksheet As Worksheet
Set myWorksheet = Worksheets("Aisle Hours")
Dim rng As Range
Set rng = myWorksheet.ListObjects("AisleHours").Range
Dim LastRow As Long
LastRow = rng.Find(What:=” * ”, _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = DateBox1.Text
rng.Parent.Cells(LastRow + 1, 2).Value = DayCombo.Text
rng.Parent.Cells(LastRow + 1, 3).Value = LoadTypeCombo.Text
rng.Parent.Cells(LastRow + 1, 4).Value = TextBox16.Text
rng.Parent.Cells(LastRow + 1, 5).Value = TextBox17.Text
rng.Parent.Cells(LastRow + 1, 6).Value = TextBox18.Text
rng.Parent.Cells(LastRow + 1, 7).Value = TextBox19.Text
rng.Parent.Cells(LastRow + 1, 8).Value = TextBox20.Text
rng.Parent.Cells(LastRow + 1, 9).Value = TextBox21.Text
rng.Parent.Cells(LastRow + 1, 10).Value = TextBox22.Text
rng.Parent.Cells(LastRow + 1, 11).Value = TextBox23.Text
rng.Parent.Cells(LastRow + 1, 12).Value = TextBox24.Text
rng.Parent.Cells(LastRow + 1, 13).Value = TextBox25.Text
rng.Parent.Cells(LastRow + 1, 14).Value = TextBox26.Text
rng.Parent.Cells(LastRow + 1, 15).Value = TextBox27.Text
rng.Parent.Cells(LastRow + 1, 16).Value = TextBox28.Text
rng.Parent.Cells(LastRow + 1, 17).Value = TextBox29.Text
rng.Parent.Cells(LastRow + 1, 18).Value = TextBox30.Text
End Sub
Private Sub Transfer3_Click()
Dim myWorksheet As Worksheet
Set myWorksheet = Worksheets("Routining")
Dim rng As Range
Set rng = myWorksheet.ListObjects("Routining").Range
Dim LastRow As Long
LastRow = rng.Find(What:=” * ”, _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = DateBox1.Text
rng.Parent.Cells(LastRow + 1, 2).Value = DayCombo.Text
rng.Parent.Cells(LastRow + 1, 3).Value = LoadTypeCombo.Text
rng.Parent.Cells(LastRow + 1, 4).Value = ComboBox1.Text
rng.Parent.Cells(LastRow + 1, 5).Value = ComboBox2.Text
rng.Parent.Cells(LastRow + 1, 6).Value = ComboBox3.Text
rng.Parent.Cells(LastRow + 1, 7).Value = ComboBox4.Text
rng.Parent.Cells(LastRow + 1, 8).Value = ComboBox5.Text
rng.Parent.Cells(LastRow + 1, 9).Value = ComboBox6.Text
rng.Parent.Cells(LastRow + 1, 10).Value = ComboBox7.Text
rng.Parent.Cells(LastRow + 1, 11).Value = ComboBox8.Text
rng.Parent.Cells(LastRow + 1, 12).Value = ComboBox9.Text
rng.Parent.Cells(LastRow + 1, 13).Value = ComboBox10.Text
rng.Parent.Cells(LastRow + 1, 14).Value = ComboBox11.Text
rng.Parent.Cells(LastRow + 1, 15).Value = ComboBox12.Text
rng.Parent.Cells(LastRow + 1, 16).Value = ComboBox13.Text
rng.Parent.Cells(LastRow + 1, 17).Value = ComboBox14.Text
rng.Parent.Cells(LastRow + 1, 18).Value = ComboBox15.Text
End Sub
Private Sub Transfer4_Click()
Dim myWorksheet As Worksheet
Set myWorksheet = Worksheets("Truck Times")
Dim rng As Range
Set rng = myWorksheet.ListObjects("TruckTimes").Range
Dim LastRow As Long
LastRow = rng.Find(What:=” * ”, _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = DateBox1.Text
rng.Parent.Cells(LastRow + 1, 2).Value = DayCombo.Text
rng.Parent.Cells(LastRow + 1, 3).Value = LoadTypeCombo.Text
rng.Parent.Cells(LastRow + 1, 4).Value = TextBox62.Text
rng.Parent.Cells(LastRow + 1, 5).Value = TextBox63.Text
rng.Parent.Cells(LastRow + 1, 6).Value = TextBox64.Text
rng.Parent.Cells(LastRow + 1, 7).Value = TextBox65.Text
rng.Parent.Cells(LastRow + 1, 8).Value = TextBox66.Text
End Sub
Private Sub Transfer5_Click()
Dim myWorksheet As Worksheet
Set myWorksheet = Worksheets("Total Hours")
Dim rng As Range
Set rng = myWorksheet.ListObjects("TotalHours").Range
Dim LastRow As Long
LastRow = rng.Find(What:=” * ”, _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = DateBox1.Text
rng.Parent.Cells(LastRow + 1, 2).Value = DayCombo.Text
rng.Parent.Cells(LastRow + 1, 3).Value = LoadTypeCombo.Text
rng.Parent.Cells(LastRow + 1, 4).Value = TextBox67.Text
rng.Parent.Cells(LastRow + 1, 5).Value = TextBox68.Text
rng.Parent.Cells(LastRow + 1, 6).Value = TextBox69.Text
rng.Parent.Cells(LastRow + 1, 7).Value = TextBox70.Text
rng.Parent.Cells(LastRow + 1, 8).Value = TextBox71.Text
End Sub
Private Sub Transfer6_Click()
Dim myWorksheet As Worksheet
Set myWorksheet = Worksheets("Comments")
Dim rng As Range
Set rng = myWorksheet.ListObjects("Comments").Range
Dim LastRow As Long
LastRow = rng.Find(What:=” * ”, _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = DateBox1.Text
rng.Parent.Cells(LastRow + 1, 2).Value = DayCombo.Text
rng.Parent.Cells(LastRow + 1, 3).Value = LoadTypeCombo.Text
rng.Parent.Cells(LastRow + 1, 4).Value = TextBox72.Text
End Sub
Attachments
Last edited by a moderator: