# PowerPoint VBA - Reference active command button



## dshort (Jun 14, 2015)

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


----------



## dshort (Jun 19, 2015)

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.)


----------

