I am making a userform that grabs data from a data sheet and puts it into a table:
- Grabs the data based on what the user wants (Brand -> Items for brand)
- Allows multiple items to be added
- Displays the info about the items
- Allows the user to specify how many of each item (for when the data is put into the inventory table)
I am just looking for any suggestions on how I could make my code better, specifically with error handling and lowering memory footprint. I am a novice, so some of this code could have better approaches. If so, please tell me.
UserForm:
ThisWorkBook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.AutoCorrect.AutoFillFormulasInLists = True
End Sub
Private Sub Workbook_Open()
Application.AutoCorrect.AutoFillFormulasInLists = False
End Sub
Add Item Button:
Private Sub cbAddItemUserForm_Click()
ufItemAdd.Show
End Sub
UserForm:
Public brand_edit As Variant
Public cBook As Workbook
Public cSheet As Worksheet
Public dSheet As Worksheet
Public specLink As Variant
Public itemAddress As Variant
Public itemID As String
Public inventoryTable As ListObject
Public x As Long
Public quantity As String
Private Sub MultiPage1_Change()
End Sub
Private Sub UserForm_Activate()
Me.tbQuantity.Text = "1"
End Sub
Public Sub cmbBrand_Change()
Me.tbQuantity.Text = "1"
Dim brand As Variant
brand = cmbBrand.Value
brand_edit = Replace(brand, " ", "_")
brand_edit = Replace(brand_edit, """", "")
brand_edit = Replace(brand_edit, "-", "")
brand_edit = Replace(brand_edit, "(", "")
brand_edit = Replace(brand_edit, ")", "")
brand_edit = Replace(brand_edit, "&", "and")
brand_edit = Replace(brand_edit, ".", "")
brand_edit = Replace(brand_edit, ",", "")
brand_edit = Replace(brand_edit, ", ", "_")
brand_edit = Replace(brand_edit, "__", "_")
brand_edit = LCase(brand_edit)
'On Error Resume Next
'If brand_edit = "" Then
' cmbItemID.RowSource = ""
'Else
On Error Resume Next
If Err = 380 Then
Exit Sub
Else
cmbItemID.RowSource = brand_edit
End If
Err.Clear
On Error GoTo 0
cmbItemID.Text = ""
End Sub
Private Sub cmbItemID_Change()
Me.tbQuantity.Text = "1"
Dim brandTable As String
Dim i As Long
Dim dataTable As ListObject
Set cBook = ActiveWorkbook
Set cSheet = cBook.Sheets("Gen. Info")
Set dSheet = cBook.Sheets("DATA")
itemID = cmbItemID.Value
brandTable = brand_edit
On Error Resume Next
Set dataTable = dSheet.ListObjects(brand_edit)
For i = 1 To dataTable.ListRows.Count
If dataTable.ListColumns(1).DataBodyRange.Rows(i) = itemID Then
tbDescription.Text = dataTable.ListColumns(3).DataBodyRange.Rows(i).Value
tbSpecs.Text = dataTable.ListColumns(4).DataBodyRange.Rows(i).Formula
specLink = dataTable.ListColumns(4).DataBodyRange.Rows(i).Formula
tbListPrice.Text = dataTable.ListColumns(5).DataBodyRange.Rows(i).Value
tbCost.Text = dataTable.ListColumns(6).DataBodyRange.Rows(i).Value
tbNotes.Text = dataTable.ListColumns(7).DataBodyRange.Rows(i).Value
itemAddress = dataTable.ListColumns(1).DataBodyRange.Rows(i).Address
tbAddress.Text = itemAddress
Exit For
Else
End If
Next
End Sub
Private Sub cbSpecs_Click()
Dim specLink_edit As Variant
specLink_edit = Replace(specLink, "=HYPERLINK(", "")
specLink_edit = Replace(specLink_edit, ")", "")
specLink_edit = Replace(specLink_edit, ",", "")
specLink_edit = Replace(specLink_edit, """", "")
specLink_edit = Replace(specLink_edit, "Specs", "")
If specLink_edit = "" Then
Exit Sub
Else
cBook.FollowHyperlink (specLink_edit)
End If
End Sub
Private Sub cbSubmit_Click()
Dim i As Long
Dim v As Variant
Dim vTable() As Variant
'add error handling here (if no cmbBrand change has occured, hitting submit will error)
Set inventoryTable = cSheet.ListObjects("inventory_table")
colItemID = inventoryTable.ListColumns("Item #").Index
colSpecs = inventoryTable.ListColumns("Specs").Index
colQty = inventoryTable.ListColumns("Qty").Index
v = inventoryTable.DataBodyRange.Rows
ReDim vTable(1 To UBound(v, 1), 1 To 5)
For i = 0 To lbItemList.ListCount - 1
vTable(i + 1, 1) = "=DATA!" & lbItemList.List(i, 2)
vTable(i + 1, 5) = lbItemList.List(i, 3)
If specLink = "" Then
ElseIf specLink <> "" Then
vTable(i + 1, 4) = lbItemList.List(i, 1)
End If
inventoryTable.ListColumns("Item #").DataBodyRange(i + 1, colItemID).Value = vTable(i + 1, 1)
inventoryTable.ListColumns("Specs").DataBodyRange(i + 1).Value = vTable(i + 1, 4)
inventoryTable.ListColumns("Qty").DataBodyRange(i + 1).Value = vTable(i + 1, 5)
Next
Unload Me
End Sub
Private Sub cbAddItem_Click()
quantity = Me.tbQuantity.Text
If Me.lbItemList.ListCount = 0 Then
x = 0
End If
With Me.lbItemList
Me.lbItemList.ColumnCount = 4
.AddItem
.List(x, 0) = itemID
.List(x, 1) = specLink
.List(x, 2) = itemAddress
.List(x, 3) = quantity
x = x + 1
End With
End Sub
Private Sub cbRemoveItems_Click()
For intCount = lbItemList.ListCount - 1 To 0 Step -1
If lbItemList.Selected(intCount) Then lbItemList.RemoveItem (intCount)
Next intCount
End Sub