Generate Event Name then run it

MartinS

Active Member
Joined
Jun 17, 2003
Messages
487
Office Version
  1. 365
Platform
  1. Windows
I have written an Excel application that, amonst others, has some radio button controls. When populating the cntrols, I want to be able to run the click event for the control whose value is True. I have three radio buttons in a group, so only one can be set at any one tiome, and I am able to set this from an associated worksheet cell, but cannot remember if it's possible to run that event once you know it's name.
For example, I have found the control on the for that's value is True,
Code:
'\* Define loop criteria...
For Each CTL In Me.Controls
'\* Set userform control association...
With CTL
'\* If control is an otion button...
If TypeName(CTL) = "OptionButton" Then
'\* If value in range equals control caption...
If Range(.Tag) = .Caption Then
'\* set value...
.Value = True
'\* generate click event name...
strROUTINE = .Name & "_Click"
'\* run click event...
[B]Call strROUTINE[/B]
End If
End If
End With
Next CTL
Can anyone help?
Thanks
Martin
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Sorry to 'bump' this, but does anyone know if this can be done?
Thanks in advance
Martin
 
Upvote 0
The easy way would be to have one routine called from all the optionbuttons. Have that routine check which is True, and run code accordingly. Then all you have to do is call that routine in your startup code.
 
Upvote 0
Yes, I guess so - will probaby do this as it seems the easiest option.
Thanks
Martin
 
Upvote 0
Actually, maybe I'm being silly, but how can you assign the same event to several controls in VBA? As a C# developer, this is an easy job, but don't think I've ever done this in VBA.
Do you mean write a procedure that takes the control as an argument, and then pass each control in checking it's value?
Martin
 
Upvote 0
You can do it with a WithEvents variable and a collection, but what I actually meant was to call a separate routine from the click event of each, either passing the control, or just checking in the routine which control is True.
 
Upvote 0
Ah OK - I am already doing this for TextBoxes on the form, as the evets for each are identical in kst cases, but the code for each option button is different, which is why i hadn't gone down that road. I was guessing that's what you might have meant - thanks for clarifying though
Regards
Martin
 
Upvote 0
I did have a brief thought that you could use Application.Run with the form name, control name and "_Click" but it seems to just produce an error about "unable to set focus to the control" or something similar.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,831
Members
449,051
Latest member
excelquestion515

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