'This subroutine will spring into action when the user changes the
'Category combo box. It clears the Make combo box,
'updates the Advanced Filter criteria based on the user's category selection,
'filters the data to reflect makes linked to the selected category,
'and populates the Make combo box with the filtered data.
Private Sub cboCategory_Change()
'Creates a string variable, Category, that stores the value of
'the Category selected by the user.
Dim Category As String
'Sets the Category variable to the category value selected by the user
Category = cboCategory.Value
'Clears the filter criteria (row 2 in the Data worksheet).
Worksheets("Data").Range("A2:C2").Clear
'Sets the filter criteria based on the new selection.
'Don 't worry if the code looks complicated to you, just focus on the key parts:
'The range is where the formula will be written
'i.e. in cell A2 on the Data worksheet)
'and the Category variable
Worksheets("Data").Range("A2").Formula = _
"=" & Chr(34) & "=" & Chr(34) & "&" & Chr(34) & Category & Chr(34)
'Clears the range where the results will be written
Worksheets("Data").Range("G3:H26").Clear
'Uses the Advanced Filter to create a unique set of make values that
'are linked to the selected category. Read the first part of the series,
'"Populating Cascading Combo Boxes Part1" for details
Worksheets("Data").Range("$A$3:$B$26").AdvancedFilter _
Action:=xlFilterCopy, _
criteriarange:=Worksheets("Data").Range("A1:B2"), _
CopyToRange:=Worksheets("Data").Range("G3"), _
Unique:=False
'Clears the contents of the Make combo box, and sets the selected value
'to nothing. This prepares it to be populated by the newly filtered
'set of values for make.
cboMake.Clear
cboMake.Value = ""
'Uses a loop to step through each cell in the new make list
'and adds each make to the combo box.
'If a value is blank, it is not added.
For Each cell In Worksheets("Data").Range("H4:H26")
If IsEmpty(cell) = False Then
cboMake.AddItem cell.Value
End If
Next cell
End Sub
'This subroutine will spring into action when the user changes the
'Make combo box.
'It updates the Advanced Filter criteria based on the user's
'category and make selections,
'filters the data to reflect models linked to the selected category and make,
'and populates the D column in the report with the resulting list of models.
Private Sub cboMake_Change()
'Creates string variables, Category and Make, to store the values of
'the Category and Make selected by the user.
Dim Category As String
Dim Make As String
'Sets the Category variable
'and Make variable to the values selected by the user
Category = Worksheets("Report").cboCategory.Value
Make = Worksheets("Report").cboMake.Value
'Sets the filter criteria.
'Read the post for details on this formula
Worksheets("Data").Range("A2").Formula = _
"=" & Chr(34) & "=" & Chr(34) & "&" & Chr(34) & Category & Chr(34)
Worksheets("Data").Range("B2").Formula = _
"=" & Chr(34) & "=" & Chr(34) & "&" & Chr(34) & Make & Chr(34)
'Clears the previous list of models
Worksheets("Data").Range("J3:L26").Clear
'Uses the Advanced Filter to create a unique set of model values that
'are linked to the selected Category. Read the first part of the series,
'"Populating Cascading Combo Boxes Part1" for details
Worksheets("Data").Range("$A$3:$C$26").AdvancedFilter _
Action:=xlFilterCopy, _
criteriarange:=Worksheets("Data").Range("A1:C2"), _
CopyToRange:=Worksheets("Data").Range("J3"), _
Unique:=True
'Copies the resulting list of models from the filtered list in the Data
'worksheet to the Report worksheet (column D)
Worksheets("Data").Range("L4:L26").Copy
Sheets("Report").Select
Range("D2").Select
ActiveSheet.Paste
Range("D2").Select
End Sub