Determine which CommandBar button that started a macro using VBA in Microsoft Excel

Let the macros themselves determine which CommandBar button that started them.
If you attach the macro below to multiple CommandBar buttons, the messagebox will display different contents:

Sub DummyMacro()
    If Application.CommandBars.ActionControl Is Nothing Then 
    ' the macro was not started from a commandbar button
        MsgBox "This could be your macro running!", vbInformation, _
            "This macro was not started from a CommandBar button"
    Else ' the macro was started from a commandbar button
        MsgBox "This could be your macro running!", vbInformation, _
            "This macro was started from this CommandBar button: " & _ 
            Application.CommandBars.ActionControl.Caption
    End If
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.