Tarkemelion
New Member
- Joined
- Jun 28, 2022
- Messages
- 21
- Office Version
- 365
- Platform
- Windows
Hi everyone,
I have a Userform where information is entered by the user. Once they click the cmdAdd button, the code should create a new row in the table and then copy the user added information into the relevant columns. When adding ListBoxes to my Userform, my code no longer works as intended (Excel crashes) and I'm wondering if I need to treat ListBox information differently from TextBox information. Have a look at my code and let me know where I'm going wrong.
Cheers!
The items with ListBoxes are "ShiftState", "CompanyState" and "CodeState". I have additional code that is trying to capture the ListBox variable for each one as follows:
I have a Userform where information is entered by the user. Once they click the cmdAdd button, the code should create a new row in the table and then copy the user added information into the relevant columns. When adding ListBoxes to my Userform, my code no longer works as intended (Excel crashes) and I'm wondering if I need to treat ListBox information differently from TextBox information. Have a look at my code and let me know where I'm going wrong.
Cheers!
VBA Code:
Private Sub cmdAdd_Click()
'Defines the variables
Dim Description As String
Dim Docket_Number As String
Dim Rate As String
Dim Quantity As String
Dim Unit As String
Dim Addition As String
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Cost Detail")
Dim tbl As ListObject
Set tbl = ws.ListObjects("Table1")
'Sets variables to UserForm values
Description = TextBoxDescription.Text
Docket_Number = TextBoxDocket.Text
Rate = TextBoxRate.Text
Quantity = TextBoxQuantity.Text
Unit = TextBoxUnit.Text
Addition = TextBoxAddition.Text
Dim NewRow As ListRow
Set NewRow = tbl.ListRows.Add
With NewRow
.Range(1) = ""
.Range(2) = ""
.Range(3) = ""
.Range(4) = ShiftState
.Range(5) = CompanyState
.Range(6) = Description
.Range(7) = Docket_Number
.Range(8) = CodeState
.Range(9) = Rate
.Range(10) = Quantity
.Range(11) = Unit
.Range(12) = Addition
End With
'Closes UserForm
Unload Me
End Sub
The items with ListBoxes are "ShiftState", "CompanyState" and "CodeState". I have additional code that is trying to capture the ListBox variable for each one as follows:
VBA Code:
'Store Company Name from List
Private Sub ListBoxCompany_AfterUpdate()
Dim CompanyState As String
CompanyState = Me.ListBoxCompany
End Sub