ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how to assign sub routines to events (https://www.excelbanter.com/excel-programming/340516-how-assign-sub-routines-events.html)

mrengc

how to assign sub routines to events
 
hello,

I was wondering if it is possible to assign a custom routine to a click
event for example? More importantly, is it possible to pass args with these
custom events?

Basically I have 30 buttons that do essentially the same thing. They are
only different depending on which button you press. So if I can create a
custom routine which passes the button number that would be ideal.



JNW

how to assign sub routines to events
 
Have all of the buttons reference the same sub or routine and have the action
based on the name of the button.



"mrengc" wrote:

hello,

I was wondering if it is possible to assign a custom routine to a click
event for example? More importantly, is it possible to pass args with these
custom events?

Basically I have 30 buttons that do essentially the same thing. They are
only different depending on which button you press. So if I can create a
custom routine which passes the button number that would be ideal.



Bob Phillips[_6_]

how to assign sub routines to events
 
Add this code to the sheet with the buttons on

Option Explicit

Dim mcolEvents As Collection

Private Sub Worksheet_Activate()
Dim cBtnEvents As clsActiveXEvents
Dim shp As Shape

Set mcolEvents = New Collection

For Each shp In Me.Shapes
If shp.Type = msoOLEControlObject Then
If TypeOf shp.OLEFormat.Object.Object Is MSForms.CommandButton
Then
Set cBtnEvents = New clsActiveXEvents
Set cBtnEvents.mButtonGroup = shp.OLEFormat.Object.Object
mcolEvents.Add cBtnEvents
End If
End If
Next

End Sub


and then insert a class module, call it clsActiveXEvents, with this code

Option Explicit

Public WithEvents mButtonGroup As MSForms.CommandButton

Private Sub mButtonGroup_Click()
MsgBox mButtonGroup.Caption & " has been pressed"
End Sub


--
HTH

Bob Phillips

"JNW" wrote in message
...
Have all of the buttons reference the same sub or routine and have the

action
based on the name of the button.



"mrengc" wrote:

hello,

I was wondering if it is possible to assign a custom routine to a click
event for example? More importantly, is it possible to pass args with

these
custom events?

Basically I have 30 buttons that do essentially the same thing. They

are
only different depending on which button you press. So if I can create

a
custom routine which passes the button number that would be ideal.






All times are GMT +1. The time now is 02:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com