Assign a Macro to a Button

Assign a Macro to a Button

In Ear­lier ses­sion we saw how to add a but­ton to the top menu of the Excel file (How to Get Access to the Tool­Bar)

Excel-VBA-MACRO-Assign a Macro to a button — 1

Excel-VBA-MACRO-Assign a Macro to a but­ton — 1

In the Code win­dow we have cre­ated two sub routines

Sub Assign_Macro1 ()

.…Some Code.…

End Sub

Sub Assign_Macro2 ()

.…Some Code.…

End Sub

Excel-VBA-MACRO -Assign a macro to a button -1

Excel-VBA-MACRO –Assign a macro to a but­ton –2

Now you can assign it to button .

To get the But­ton on the Excel, Click and Hold the But­ton icon in the Top menu of the Excel and Then Click + Hold + drag  in Excel Sheet (Work­Sheet) to give it proper size.

For the very first time when you cre­ate a but­ton another win­dow will prompt auto­mat­i­cally to assign Macro to that button

Excel-VBA-MACRO-Assign a Macro to a button— 3

Excel-VBA-MACRO-Assign a Macro to a but­ton— 3

You can click can­cel and Assign the Macro later to this Button

you can see that Sheet1.Assign_Macro1 and Sheet1.Assign_Macro2 macro’s are dis­played, you can select any of these and click OK. Now when next time when you click the but­ton the select sub rou­tine will be called.

You can change the Assigned Macro or re-size the but­ton or Edit the Text on the but­ton or any other kind of for­mat­ting by right click on the but­ton and select the appro­pri­ate option from the menu.

Excel-VBA-MACRO-Assign a Macro to a button-4

Excel-VBA-MACRO-Assign a Macro to a button-4

 

Happy Macro­ing :)

Sumit Jain

2 thoughts on “Assign a Macro to a Button

  1. Vishal Tulsiyan

    i cre­ated the but­tons. how­ever, in my code sheet (alt+f11) i am still not able to see any new sub rou­tines created.

    on the right side (code win­dow) i can just see this:

    Pri­vate Sub Worksheet_SelectionChange(ByVal Tar­get As Range)

    End Sub

    1. sumitjain Post author

      Hello Vishal,

      Sorry for one offline ques­tion, r u the one who stud­ied from chen­nai and stayed at medavakkam, if not please ignore.

      Answer to your ques­tion: First you have to cre­ate a sub rou­tine in the code win­dow and then assign it to but­ton , not the other way around.
      please let me know if u still not able to resolve it , it will give the detail descrip­tion then :)

      Thanks
      Sumit

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

%d bloggers like this: