Forum

Userform subscript ...
 
Notifications
Clear all

Userform subscript out of range

3 Posts
2 Users
0 Reactions
171 Views
(@m1cks)
Posts: 2
New Member
Topic starter
 

I found a great video on making userforms from this site and have built almost what I want but when it comes to executing the form I keep getting a

Runtime error 9 subscript out of range error - can anyone help?  I have tried to modify the code to match elements of my form but it isn't working.

I have my macro button to open the userform on a tab named "statistics" I want the data from the userform to populate a table named 'logsheet' on a different tab named "Logs"

I have changed some elements of the original video to match my sheet.  can someone help me correct it but also explain where i've gone wrong and how.

Private Sub Submit_Click()
Dim lastrow As Range
Dim LogTable As ListObject

'Add row to bottom of Logsheet Table
ActiveSheet.ListObjects("Logsheet").ListRows.Add

'Enter data from form into our new row
Set LogTable = ActiveSheet.ListObjects("Logsheet")
Set lastrow = LogTable.ListRows(LogTable.ListRows.Count).Range

With lastrow

.Cells(1, 1) = ComboBox1.value
.Cells(1, 2) = ComboBox2.value
.Cells(1, 3) = ComboBox3.value
.Cells(1, 4) = ComboBox4.value
.Cells(1, 5) = ComboBox5.value
.Cells(1, 6) = ComboBox9.value
.Cells(1, 7) = ComboBox6.value
.Cells(1, 8) = ComboBox10.value
.Cells(1, 9) = ComboBox11.value
.Cells(1, 10) = ComboBox7.value
.Cells(1, 11) = ComboBox8.value
.Cells(1, 15) = TextBox1.value
.Cells(1, 16) = TextBox2.value

If SSLUsed.value = True Then

.Cells(1, 12) = "Yes"

Else

.Cells(1, 12) = "No"

End If

If FLUsed.value = True Then

.Cells(1, 13) = "Yes"

Else

.Cells(1, 13) = "No"

End If

If finaldest.value = True Then

.Cells(1, 14) = "Yes"

Else

.Cells(1, 14) = "No"

End If

End With

End Sub

 
Posted : 26/05/2022 4:26 pm
(@debaser)
Posts: 837
Member Moderator
 

From your description, the table is not on the active sheet, which would cause the error. You could use:

 

Private Sub Submit_Click()

Dim LogTable As ListObject

'Add row to bottom of Logsheet Table
Set LogTable = Sheets("Log").ListObjects("Logsheet")

With LogTable.ListRows.Add.Range

.Cells(1, 1) = ComboBox1.value

' rest of the code

 
Posted : 27/05/2022 4:41 am
(@m1cks)
Posts: 2
New Member
Topic starter
 

Thats great Velouria.  Problem solved

 
Posted : 27/05/2022 3:26 pm
Share: