PowerPoint VBA - Reference active command button

dshort

New Member
Joined
Jun 14, 2015
Messages
2
This should be so easy, but I can't figure it out. I have a PowerPoint slide with a command button (Button1). When I click the button, I want VBA to call another subroutine (ChangeColor) that changes the button's color depending on several conditions. I do not want to harcode the button's name in ChangeColor since several other buttons may call that same routine. Instead, I need ChangeColor to determine which button was just clicked (is active or has focus) and then change its color.

A sample of my code is below. The part I can't figure out is in brackets. Can someone please help? Thanks!

Private Sub Button1_Click()
Call ChangeColor
End Sub

Private Sub ChangeColor()
[How do I reference the currently active button here?].BackColor = RGB(0, 176, 80)
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I just figured out the answer to my own question: use a global variable to store the button name! Here is the code:

In a module, create a global variable to store the button name:

Global vButton As Object

Then reference the global variable in the private subs:

Private Sub Button1_Click()
Set vButton = Me.Button1
Call ChangeColor
End Sub

Private Sub Button2_Click()
Set vButton = Me.Button2
Call ChangeColor
End Sub

Private Sub ChangeColor()
vButton.BackColor = RGB(0, 176, 80)
End Sub

In my real program, the ChangeColor sub has many more lines of code that change the button to different colors depending on certain conditions. So you can see where it is more efficient to use a global variable rather than repeating all of those lines of code for each button. (There are 14 buttons on my slide, all of which behave the same way when clicked.)
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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