DetroitDavid
Board Regular
- Joined
- Jul 20, 2013
- Messages
- 211
I have a macro workbook with two buttons on a worksheet (on the way to 200) and for testing I am trying to get the button to (1) set its value to a name, and then (2) use that name to show in both a msgbox and tooltip. Eventually this will control a large Select Case statement with the named value.
I want to be able to call this in VBA and use names, so I can’t use plain form buttons, I’ve discovered that they need to be ActivX buttons.
I saw a post here by VoG in 2002 that states:
This seems similar to what I want; is there a solution?
The working code for this is below.
I’m looking to try and condense this is that it is more compact. Rather than have potentially 400 subroutines on this page, can I somehow “call” the button, or send its name with the click event and have a more elegant piece of code?
TIA-DD
I want to be able to call this in VBA and use names, so I can’t use plain form buttons, I’ve discovered that they need to be ActivX buttons.
I saw a post here by VoG in 2002 that states:
“If you wanted a generic one line of code for all buttons then you would need to use Forms buttons. ActiveX buttons do not have the Application.Caller property that you could access.”
This seems similar to what I want; is there a solution?
The working code for this is below.
I’m looking to try and condense this is that it is more compact. Rather than have potentially 400 subroutines on this page, can I somehow “call” the button, or send its name with the click event and have a more elegant piece of code?
Code:
'I WOULD LIKE SOMETHING LIKE THIS...
'Private Sub CommandButton()
''' btn_Name = ???
' Get button name and use as a variable
'_Click
' Set_Agency_Number = Me.OLEObjects(btn_Name).Object.Caption
' MsgBox Set_Agency_Number
'_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' For Each objTTL In ActiveSheet.OLEObjects
' fTTL = objTTL.Name = "TTL"
' Next objTTL
' If Not fTTL Then CreateToolTipLabel btn_001, "001 - Button number 001"
'End Sub
Code:
'THIS BLOCK OF CODE WORKS, BUT IS TOO MANY SUBs...
Public Set_Agency_Number_text As String
Private Sub btn_001_Click()
Set_Agency_Number = Me.OLEObjects("btn_001").Object.Caption
MsgBox Set_Agency_Number
End Sub
Private Sub btn_002_Click()
Set_Agency_Number = Me.OLEObjects("btn_002").Object.Caption
MsgBox Set_Agency_Number
End Sub
Private Sub btn_001_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim objTTL As OLEObject, fTTL As Boolean
For Each objTTL In ActiveSheet.OLEObjects
fTTL = objTTL.Name = "TTL"
Next objTTL
If Not fTTL Then CreateToolTipLabel btn_001, "001 - Button number 001"
End Sub
Private Sub btn_002_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim objTTL As OLEObject, fTTL As Boolean
For Each objTTL In ActiveSheet.OLEObjects
fTTL = objTTL.Name = "TTL"
Next objTTL
If Not fTTL Then CreateToolTipLabel btn_002, "002 - Balajie HmHC (SYN)"
End Sub
TIA-DD