Macros and Option Buttons

rob1987

New Member
Joined
Feb 14, 2011
Messages
39
Hello,

I have a spreadsheet with 50 rows of data in - at the end of each row is an option button which, when pressed, runs a macro depending on set criteria. At the moment I have to go through all option buttons manually to press them. I would like to insert an overall command button that when pressed will in effect select each option button and run each macro. Can somebody please advise how to do this?

I have tried linking them all through one cell (and I have tried to do this with checkboxes too). When I tried with checkboxes I can either check or uncheck them all via the one box - but I can't get the macro to run when they are checked.

Any advise on the best way to do this would be greatly appreciated. Many Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this:-
The code sets each "OptionButton " to true and will run the Code in each OptionButton_Click Event.
Code:
Dim Shp As OLEObject
For Each Shp In ActiveSheet.OLEObjects
    If TypeName(Shp.Object) = "OptionButton" Then
        Shp.Object = True
    End If
Next Shp
 
Upvote 0
Hi Mike, thanks for the reply.

I tried your code but it says "invalid outside procedure". Do I need to edit any of it to reflect my worksheet names or anything?

Thanks for your help
 
Upvote 0
Sorry it didn't want assigning to the sheet, I see, my fault.

However it now says Run time error 438. Does it need to be an ActiveX Option Button or something?
 
Upvote 0
What does the code for each button do?

Is it basically the same for each one with only things like the row/range involved changing?

If that's what's happening you could set up a separate sub that can be called from each button.

You would create the sub so that it takes a parameter(s)/argument(s) that indicate which row/range to work on.

So you could call that sub from each option button's click event.

For the 'all' button you could set it up so the called sub replicates whatever would happen if all the buttons were pressed.
 
Upvote 0
You need to be running it from a sheet control, like a CommndButton on the sheet, like this:-
Code:
Private Sub CommandButton1_Click()
Dim Shp As OLEObject
For Each Shp In ActiveSheet.OLEObjects
    If TypeName(Shp.Object) = "OptionButton" Then
        Shp.Object = True
    End If
Next Shp
End Sub
or perhaps a basic module , like this:-
Code:
sub Mycode()
Dim Shp As OLEObject
For Each Shp In ActiveSheet.OLEObjects
    If TypeName(Shp.Object) = "OptionButton" Then
        Shp.Object = True
    End If
Next Shp
End Sub
If you not familier with those try running from a Worksheet event as follow:-
Right click sheet tab:- Select "ViewCode", VB window appears .
Paste code below into VB Window.
Close VB Window
Double Click in cell "A1".
Code should now run to Option Buttons.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim Shp As OLEObject
If Target.Address(0, 0) = "A1" Then
For Each Shp In ActiveSheet.OLEObjects
    If TypeName(Shp.Object) = "OptionButton" Then
        Shp.Object = True
    End If
Next Shp
End If
End Sub
Mick
 
Upvote 0
Hi Norie,

Each option button has very similar code but it just refers to a different range in each one.

I'll have a look at what you've put and also Micks to see which is best.

Many Thanks
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,758
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top