ToggleButton's event Application.Caller

drom

Well-known Member
Joined
Mar 20, 2005
Messages
543
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and thanks in advance!

I have like 25/30 ToggleButton_Change events on 1 WorkSheet.


eg:

ToggleButton1_Change
ToggleButton2_Change
ToggleButton3_Change
ToggleButton4_Change and so on.


is there any way to know to which ToogleButton it's been attached the

Private Sub ToggleButton9_Change() events????


kind of:

Code:
Private Sub ToggleButton9_Change()
   Debug.Print Application.Caller ???
   Debug.Print ToggleButton9.Name 'Work's Fine but I have to use some thing more generic
     'to use it in 25/30 ToggleButtons
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Your post is a bit confusing.

First, I think you used a misnomer with the word "events" when you wrote

"I have like 25/30 ToggleButton_Change events..."

I think you meant to say "controls".

But then you ask about Application.Caller which is a Forms control property and your code looks like you are using activex ToggleButton controls with a Change event. Speaking of, there are only 14 events with embedded activex ToggleButton controls, not 25/30 so what you probably need is a class module to ID the names instead of Application.Caller which will do you no good for activex.

Here is a link showing how to identify an activex control's name using a class module, which you can adapt for ToggleButtons:

http://www.mrexcel.com/forum/showthread.php?t=347430
 
Upvote 0
I have over 20 Private Sub CheckBox1_Click() macros on a Sheet named "Sheet01"
the name's of this over 20 buttons which appear on the sheet's name Box, when you are on design mode and right click on the buttons are:

ToggleButton1
ToggleButton2
ToggleButton3
ToggleButton4 and so on

the ToggleButton are pasted using the ActiveX controls

When I click any of this buttons on design mode, I get the wollowing macros:

Code:
Private Sub ToggleButton100_Click()

End Sub


Code:
Private Sub ToggleButton8_Click()

End Sub


so How can I know If I click the ToggleButton100 on the Sheet named "Sheet01"

the name Of this ToggleButton on a message box. Kind of:

Code:
Private Sub ToggleButton100_Click()
 msgbox "ToggleButton100"
'But using code like eg:
MsgBox ClickedToggleButton.name   ????????????????
Debug.Print ClickedToggleButton.name ????????????????
End Sub

Thanks!
 
Upvote 0
SOLVED: ToggleButton's event Application.Caller

Code:
    For Each oleCtl In Worksheets("Sheet1").OLEObjects
      If TypeOf oleCtl.Object Is msforms.ToggleButton Then
    .......................
......................

gets the Collection of names but just one as requested??
 
Upvote 0
Here is the code from the link I posted, slightly modified to fit your situation for toggle buttons.

In a new fresh standard module paste in this entire code, making sure that if you already have an existing AutoOpen macro, you delete it and replace it with this:


Code:
Public myControls As Collection
 
 
Sub Auto_Open()
With Application
.ScreenUpdating = False
Dim oleCtl As OLEObject, ctl As Class1
Set myControls = New Collection
For Each oleCtl In Worksheets("Sheet1").OLEObjects
If TypeOf oleCtl.Object Is msforms.ToggleButton Then
Set ctl = New Class1
Set ctl.myTglBtn = oleCtl.Object
myControls.Add ctl
End If
Next
.ActiveWindow.WindowState = xlMaximized
.Goto Worksheets("Sheet1").Range("A1"), True
End With
End Sub


From the VBE's menu bar, click Insert Class Module. Accept the class module's default name of Class1.

In your new Class1 module, paste this in:

Code:
Public WithEvents myTglBtn As msforms.ToggleButton
 
 
Private Sub myTglBtn_Click()
MsgBox "Hello, my name is ''" & myTglBtn.Name & "''." & vbCrLf & _
"My caption is ''" & myTglBtn.Caption & "''.", 64, _
"You just clicked me:"
End Sub

Save the workbook.

Close the workbook.

Open the workbook.

Test by clicking a toggle button control on Sheet1. It works for me, no problem.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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