DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

1

How To Determine which CommandBar button started a macro using VBA?

In this article, you will learn how to determine which CommandBar button started a macro using VBA ** in Microsoft Excel. Let’s see them below!! Get the official version of **MS Excel from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

Example

  • Firstly, in the Excel Worksheet, you have to go to the Developer Tab.
  • Then, you need to ** ** select the Visual Basic option under the Code section.

Select Visual Basic
Select Visual Basic

  • Now, you have to copy and paste the code given below.
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

Enter fullscreen mode Exit fullscreen mode
  • You need to save the code by selecting it and then close the window.

Save the Code

  • Again, you have to go to the Excel Spreadsheet , and click on the Developer Tab.
  • Then, you need to choose the Macros option in the Code section.

Choose Macro option
Choose Macro option

  • Now, you have to make sure that your macro name is selected and click the *Run * button.

Run the Code

  • Finally, after running the macro, you will get the *output * as given below in Excel.

Output

Wrap-Up

We hope that this article gives you guidelines on how to determine which CommandBar button started a macro using VBA in Microsoft Excel. ** ** Please leave a comment in case of any queries, and don’t forget to mention your valuable suggestions as well. Thank you so much for Visiting Our Site!! Continue learning on Geek Excel!! *Read more on Excel Formulas *!!

Related Articles:

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more