Recently, I created a userform with three listboxes that are interdependent on each other. The main purpose of the userform is to allow the user to run a macro after selecting an option/value from the listboxes.
There are two worksheets in the Excel file:
The data source for the listboxes is from the above slicers.
* According to the selection from listbox3 (most right hand side), three macro will be run.
A macro was assigned to a command button, which should run the macro after clicking the button. VBA code was written to retrieve a value from the table in Column D using Vlookup and run a specific macro based on the value in the column.
However, when the macro is run, Error 13 occurs. Debugging the code revealed that the values for listbox1 to listbox3 were successfully obtained, but the value of Column D is empty, and the type of the value is String. Debugging led me to the line with "Select Case CStr(idVal)".
If more information is required, please let me know! I have tried many ways to solve the error, but I have been unsuccessful. Your help would be very meaningful to me!
Thank you!
There are two worksheets in the Excel file:
- Worksheet "LookupTable"
- Contains a pivoted table (named "TableSource") containing values for the listboxes.
Master Record.xlsm | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | |||
1 | Stage | Case | Doc1 | Doc2 | Doc3 | Doc4 | Doc5 | Doc6 | Doc7 | ||
2 | 1.Stage_1 | 1_Withdraw | Minute | Reply_Letter | |||||||
3 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | Referral_memo | Photo_affected_area | Minute | Layout+Photo | Photo_affected_area_2 | Reply_Letter | |||
4 | 1.Stage_1 | 3_>2m | Minute | Reply_Letter | |||||||
5 | 1.Stage_1 | 5_Verify ownership | Minute | ||||||||
6 | 2.Stage_2 | 1_CWT | Minute | Layout+Photo | CWT_Record | ||||||
7 | 2.Stage_2 | 3_Reply_M2 | Minute | Form_44 | |||||||
8 | 3.Stage_3 | 2_Form 44_S3 | Minute | Form_44 | |||||||
9 | 3.Stage_3 | 3_Comply | Minute | Layout+Photo_UC | Layout+Photo_C | Reply_Letter | |||||
10 | 4.Other | 4_EOT_Min | Minute | ||||||||
11 | 4.Other | 5_PO Review M1 | Minute | ||||||||
12 | 4.Other | 7_Access information | Minute | ||||||||
LookupTable |
- Worksheet "UnpivotedData"
- Contains an unpivoted data table (named "UnpivotedData_tbl") retrieved from the pivoted table in Worksheet "LookupTable" using Power Query.
- Column D is input manually, and the format of all values in column D is number.
- There are also three slicers.
Master Record.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Stage | Case | Doc | Identifier | Doc_Type | ||
2 | 1.Stage_1 | 1_Withdraw | Minute | 1.1.1 | Doc1 | ||
3 | 1.Stage_1 | 1_Withdraw | Reply_Letter | 1.1.2 | Doc2 | ||
4 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | 1.2.1 | Doc1 | |||
5 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | Referral_memo | 1.2.2 | Doc2 | ||
6 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | Photo_affected_area | 1.2.3 | Doc3 | ||
7 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | Minute | 1.2.4 | Doc4 | ||
8 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | Layout+Photo | 1.2.5 | Doc5 | ||
9 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | Photo_affected_area_2 | 1.2.6 | Doc6 | ||
10 | 1.Stage_1 | 2_REFER_TO_BD_DEFECTIVE_PIPE | Reply_Letter | 1.2.7 | Doc7 | ||
11 | 1.Stage_1 | 3_>2m | Minute | 1.3.1 | Doc1 | ||
12 | 1.Stage_1 | 3_>2m | Reply_Letter | 1.3.2 | Doc2 | ||
13 | 1.Stage_1 | 5_Verify ownership | Minute | 1.4.1 | Doc1 | ||
14 | 2.Stage_2 | 1_CWT | Minute | 1.4.2 | Doc1 | ||
15 | 2.Stage_2 | 1_CWT | Layout+Photo | 1.4.3 | Doc2 | ||
16 | 2.Stage_2 | 1_CWT | CWT_Record | 1.4.4 | Doc3 | ||
17 | 2.Stage_2 | 3_Reply_M2 | Minute | 1.5.1 | Doc1 | ||
18 | 2.Stage_2 | 3_Reply_M2 | Form_44 | 1.6.1 | Doc2 | ||
19 | 3.Stage_3 | 2_Form 44_S3 | Minute | 1.6.2 | Doc1 | ||
20 | 3.Stage_3 | 2_Form 44_S3 | Form_44 | 1.7.1 | Doc2 | ||
21 | 3.Stage_3 | 3_Comply | Minute | 1.7.2 | Doc1 | ||
22 | 3.Stage_3 | 3_Comply | Layout+Photo_UC | 1.8.1 | Doc2 | ||
23 | 3.Stage_3 | 3_Comply | Layout+Photo_C | 1.8.2 | Doc3 | ||
24 | 3.Stage_3 | 3_Comply | Reply_Letter | 2.1.1 | Doc4 | ||
25 | 4.Other | 4_EOT_Min | Minute | 2.1.2 | Doc1 | ||
26 | 4.Other | 5_PO Review M1 | Minute | 2.1.3 | Doc1 | ||
27 | 4.Other | 7_Access information | Minute | 4.7.1 | Doc1 | ||
UnpivotedData |
The data source for the listboxes is from the above slicers.
* According to the selection from listbox3 (most right hand side), three macro will be run.
A macro was assigned to a command button, which should run the macro after clicking the button. VBA code was written to retrieve a value from the table in Column D using Vlookup and run a specific macro based on the value in the column.
However, when the macro is run, Error 13 occurs. Debugging the code revealed that the values for listbox1 to listbox3 were successfully obtained, but the value of Column D is empty, and the type of the value is String. Debugging led me to the line with "Select Case CStr(idVal)".
VBA Code:
Private Sub CommandButton3_Click()
Dim lb1Val As String
Dim lb2Val As String
Dim lb3Val As String
Dim idVal As Variant
Dim LookupRange As Range
Set LookupRange = ThisWorkbook.Worksheets("UnpivotedData").Range("A1:D53")
lb1Val = Trim(ListBox1.Value)
lb2Val = Trim(ListBox2.Value)
If ListBox3.ListIndex <> -1 Then ' Check if an item is selected in ListBox3
Dim i As Long
For i = 0 To ListBox3.ListCount - 1 ' Loop through all items in ListBox3
If ListBox3.Selected(i) Then ' Check if the item is selected
If lb3Val = "" Then ' If it's the first selected item, assign the value
lb3Val = ListBox3.List(i)
Else ' If it's not the first selected item, append the value
lb3Val = lb3Val & "," & ListBox3.List(i)
End If
End If
Next i
End If
lb3Val = Trim(lb3Val) ' Move the trimming of lb3Val here
' Assign a default value to idVal
idVal = ""
' Debug prints
Debug.Print "idVal: " & idVal
Debug.Print "Type of idVal: " & TypeName(idVal)
Debug.Print "lb1Val: " & lb1Val
Debug.Print "lb2Val: " & lb2Val
Debug.Print "lb3Val: " & lb3Val
Debug.Print "Lookup values: " & lb1Val & ", " & lb2Val & ", " & lb3Val
Debug.Print "Lookup range: " & LookupRange.Address
If lb1Val <> "" And lb2Val <> "" And lb3Val <> "" Then
Debug.Print "Lookup values: " & lb1Val & ", " & lb2Val & ", " & lb3Val
' VLOOKUP function with idVal assigned a value
idVal = Application.VLookup(Array(lb1Val, lb2Val, lb3Val), LookupRange, 4, True)
If Not IsError(idVal) Then
Select Case CStr(idVal)
Case "151"
Call S1_5_1
'Add more cases as needed
Case Else
'Handle case when idVal does not match any of the expected values
End Select
Else
idVal = "Not found" 'Assign a default value if the lookup fails
End If
Else
MsgBox "Please select values in all three list boxes"
End If
If TextBox1.Value <> "" Then
Shell "explorer.exe " & TextBox1.Value, vbNormalFocus
End If
End Sub
If more information is required, please let me know! I have tried many ways to solve the error, but I have been unsuccessful. Your help would be very meaningful to me!
Thank you!