Hello Raw,
Since all the controls on the worksheet are from the Control Toolbox and not the Forms Controls, the code needs to be changed again. That's why you are getting an error at the Option Button code. The macro was written for a Forms Option Button and not a Control Toolbox Option Button.
Sub ChangeFillRange(OptBtn As MSForms.OptionButton)
'Name of the ComboBox
CboName = "ComboBox1"
Set Cbo = ActiveSheet.OLEObjects(CboName).Object
'Assign Names to the Option Buttons
Opt1 = "OptionButton1"
Opt2 = "OptionButton2"
Opt3 = "OptionButton3"
Opt4 = "OptionButton4"
'Choose the Drop Down's Fill Range based on the Option Button
Select Case OptBtn.Name
Case Opt1
FillRng = "$A$1:$A$10"
Case Opt2
FillRng = "$B$1:$B$10"
Case Opt3
FillRng = "$C$1:$C$10"
Case Opt4
FillRng = "$D$1:$D$10"
Case Else
Exit Sub
End Select
'Load the ComboBox with the Fill Range Data
Cbo.Clear
With ActiveSheet
For Each Cell In .Range(FillRng)
Cbo.AddItem Cell.Value
Next Cell
End With
End Sub
Next you need to call the macro from each Option Button's Click event code.
1. While in Excel, Press ALT+F11 (Open the VB Editor)
2. Press CTRL+R (Places the cursor in the Project Explorer window)
3. Click on the Worksheet the Option Buttons are on.
4. Press F7 to display the Code window.
5. Click on the Leftside ListBox's down arrow located above the Code Window.
6. Click on the name of the Option Button.
7. You will see the default Event Procedure (Example for OptionButton1) ...
Private Sub OptionButton1_Click()
End Sub
8. Add the macro call using the Option Button's name...
Private Sub OptionButton1_Click()
Call ChangeFillRange(OptionButton1)
End Sub
9. Repeat steps 5 to 8 for each Option Button.
Sincerely,
Leith Ross
Bookmarks