I have a listbox with multiple options to choose from on sheet1. On sheet2 I have the actual options that come up when selecting from the list box. the list box works in conjunction with the combobox to generate graphs. Certain options in the combobox only relate to particular options in the listbox. How do I write a select case function that will basically say, if you choose this one from the combobox, then only these options are available in the list box, and if you choose this other one in the combobox then only these other options are available in the listbox, and so on?
Here is the code for the combobox in question:
Sub Populate_Combobox_Worksheet()
'This line of code locks the boxes in place and prevents them from resizing
ThisWorkbook.Worksheets("sheet1").Shapes("CommandButton1").Placement = xlFreeFloating
'The Excel workbook and worksheets that contain the data, as well as the range placed on that data
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnData As Range
'Variant to contain the data to be placed in the combo box.
Dim vaData As Variant
'Initialize the Excel objects
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Sheet3")
'Set the range equal to the data, and then (temporarily) copy the unique values of that data to the L column.
With wsSheet
Set rnData = wsSheet.Range(wsSheet.Range("A1"), wsSheet.Range("A100").End(xlUp))
rnData.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=wsSheet.Range("L1"), _
Unique:=True
'store the unique values in vaData
vaData = wsSheet.Range(wsSheet.Range("L1"), wsSheet.Range("L100").End(xlUp)).Value
'clean up the contents of the temporary data storage
wsSheet.Range(wsSheet.Range("L1"), wsSheet.Range("L100").End(xlUp)).ClearContents
End With
'display the unique values in vaData in the combo box already in existence on the worksheet.
With ThisWorkbook.Worksheets("sheet1").OLEObjects("ComboBox1").Object
.Clear
.List = vaData
.ListIndex = -1
End With
End Sub
And here is the code for the listbox in question:
Sub Populate_ListBox_Worksheet2()
'This line of code locks the boxes in place and prevents them from resizing
ThisWorkbook.Worksheets("sheet1").Shapes("CommandButton1").Placement = xlFreeFloating
'The Excel workbook and worksheets that contain the data, as well as the range placed on that data
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnData As Range
'Variant to contain the data to be placed in the combo box.
Dim vaData As Variant
'Initialize the Excel objects
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Sheet3")
'Set the range equal to the data, and then (temporarily) copy the unique values of that data to the L column.
With wsSheet
Set rnData = .Range(.Range("C1"), .Range("C100").End(xlUp))
rnData.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Range("L1"), _
Unique:=True
'store the unique values in vaData
vaData = .Range(.Range("L1"), .Range("L100").End(xlUp)).Value
'clean up the contents of the temporary data storage
.Range(.Range("L1"), .Range("L100").End(xlUp)).ClearContents
End With
'display the unique values in vaData in the combo box already in existence on the worksheet.
With ThisWorkbook.Worksheets("sheet1").OLEObjects("ListBox1").Object
.Clear
.List = vaData
.ListIndex = -1
End With
End Sub
Bookmarks