Open In App

Assigning Excel Macro to Objects

Last Updated: 06 Jun, 2021

R

Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Excel, a recorded macro can be assigned to different objects like a shape, graphic, or control note. Instead of running the macro from the required tool in ribbon, we can create these objects to run them easily. They get very handy when multiple macros are there. Individual objects can be created for different macros. 

In this article, let’s discuss how can we assign macros to different objects :

1. Assigning Macro to a Shape

It is one of the easiest ways to run a macro is to create any shape in the worksheet and use it for running the macro.

The steps that need to be followed are:

  • Click on the Insert Tab on the ribbon.

  • Go to the Illustrations group and click on the Shapes icon. In the sheet, choose any shape we like and want it to be assigned as the macro.


  • Click where you want to add the shape in the sheet and the shape will automatically get inserted.

  • The shape can be resized or re-formatted accordingly to the way you want. Text can also be added in the shape.

  • Then Right-Click on the shape and a dialog box would be opened. Then click on Assign Macro.

  • After right-clicking on the shape, another dialog box of Macro would be opened.

  • In the dialog box, select the macro from the list you want to assign to your shape and click the OK button.

The work is done! Now the shape would work as a button and whenever you click on it, it will run the assigned macro.

2. Assigning a Macro to a Graphic

In Excel, there is a pool of graphics from which we can choose. There are 3-D models, Icons, pictures, shapes, and much more which can be added to the sheet as required. These graphics have a special feature i.e Macro can be assigned to them and can run macro whenever needed. 

The steps that need to be followed are:

  • Click on the Insert Tab on the ribbon.

  • From Illustrations tools, different graphics can be chosen such as shapes, icons, pictures, and 3-D models.

  • Click on any graphic and insert it into the sheet. Let’s insert a 3-D model of an insect here.

  • Then, on clicking right on the graphic, from the options, choose to assign macro.

  • A dialog box would open and select the macro you want to assign to that graphic, click OK.

Now, our graphic is ready to use. Just click on the graphic to run the assigned macro.

3. Assigning a Macro to a Control

We can also create a control button to assign a particular macro to it and when a user clicks it, the control button performs the desired set of actions. 

Let’s understand how can we implement this:

  • Go to the Developer Tab on the ribbon.

  • In the Controls group, click on the Button tool.

  • Now, click anywhere on the Excel sheet. As soon as you do this, the Assign Macro dialog box would be opened.

  • Select the macro you want to assign to that button and fill in the other options. Then click on OK.
  • After that, a rectangular button can be seen on the sheet and ready to be used.

The button which appears on the sheet has default settings, and you can’t format it like the colour, shape of the button. However, the text which appears on the button can be changed. Just a right-click on the button and a dialog box would open.

  • Click on Edit Alt Text and change the text. 

These are some objects to which a macro can be assigned. 



R

News
Improve
Discuss
Do you want to advertise with us?Click here to know more

Excel Personal Macro Workbook

article_img
When we create the macros for automation, they can be used for that particular workbook only. We cannot access them outside that workbook in others. Excel Personal Macro Workbook is a hidden workbook in your local system that pops up whenever you open the Excel application. It is a place where all the macros from any workbook can be stored and one can use the macros from here which are often required. How to create a personal macro workbook? Creating a personal macro workbook is similar to creating a macro in Excel. Let's discuss it in some simple steps : Click on the developer tab on the ribbon and then click on the Record Macro tool in the Code group.A new dialog box would open.In the option, "Store Macro In", choose Personal Macro Workbook. In this way, our macro would be stored in the personal macro workbook.After filling the dialog box, click OK.In the last, click on stop recording in the developer tab. This is how a personal macro workbook can be created. How to view, create, copy and edit macros in Personal Macro Workbook? All the options like view, create, copy and edit macros can be done from VBA(Visual Basic for Applications) Editor. Let's discuss it in some simple ste
Read More

How to Password Protect Your Excel Macro?

article_img
It is a best practice, to protect the Macro (VBA Code) from other users to edit or view. In this article, we explain to create a simple macro and protect the same "VBA Project" using a password in Excel from other users. Implementation: Follow the below steps to protect your excel macro with a password: Step 1: Open an excel file, to create a simple “hello world” macro. Step 2: Press Alt + F11 – to open Visual Basic Editor. Step 3: Click Insert >> Module, which will add a new module as in Img1. Img 1 Step 4: Type the below code in “code window”. Sub firstMacro() MsgBox "Hello World" End Sub Step 5: Right-click “VBAProject (Book1)” and then click “VBAProject Properties”. Step 6: GoTo Protection Tab >> Check “Lock project for viewing” and type your password in both Password and Confirm password input text boxes. Press “OK” Step 7: Save your Excel workbook as Excel Macro-Enabled Workbook (*.xlsm) and close Step 8: Now reopen your excel file. Press Alt+F8 – to open the Macro dialog box. Select the macro “firstMacro” and click “RUN”. Step 9: Our macro will pop up below the message box. Close or press OK to hide the message box Step 10: Press Alt + F11 – to open Visual Bas
Read More

How to Record a Macro in Excel

article_img
Excel Macro is simply a record button that records a set of actions performed on Excel that can be run multiple times. For example, if you have to format some raw data on a weekly basis you can use a macro to record yourself formatting the data once and let Excel automate the task in the future. it's used to help users save time and avoid performing redundant tasks. What is a Macro in ExcelIn simple words, a Macro is a snippet of VBA code that empowers you to execute a series of actions with a single command. Whether you're generating this code using a macro recorder or crafting it manually, it all falls under the umbrella of macros. How to Get the Excel Developer Tab Show Up in the RibbonThe first step to record a macro is to get the Developer tab turned on. If you already see the Developer tab in the ribbon, then you can directly follow the steps to record a macro. How to Enable the Developer Tab in ExcelIn order to record a macro the Developer tab on the ribbon needs to be activated as it isn't on by default within Microsoft Excel. The developer tab contains all the macro tools among various other features. How to Activate: To activate it click on the file tab> go to options
Read More

How to Enable & Run a Macro in Excel? 6 Ways

article_img
We can record a macro in Excel or write from scratch in VB Editor. Excel provides different options to run a macro. In this article, we explore four different approaches to running a macro in an Excel application. Even while executing an Excel macro is a straightforward process for seasoned users, it might not be clear to newbies. You will discover numerous ways to execute macros in this article, some of which may entirely alter the way you interact with Excel files. The below macro will write the text “Geeks for Geeks” in cell B5 Sub updateText() ActiveSheet.Range("B5").Value = "Geeks for Geeks" End Sub How to Run a Macro from Excel RibbonFollow the below steps to run a macro in the Macro dialogue box: Step 1. Go to View >> Macros >> View Macros – to pop-up Macro dialogue box [keyboard shortcut - Alt+F8]. Step 2. Select a Macro from the list (e.g. updateText) and Press “Run” Output: How to Run Macro from VBA EditorFollow the below steps to run a macro in VBE: Step 1. Open Excel Step 2. Go to Developer>> Visual Basic – to open VBA Editor [keyboard shortcut - Alt+F11] Step 3. Select anywhere in the macro code and click the “Run button” (Green Arrow) or Press F5 How
Read More

N

How to Create a Macro in Excel: Build, Run, and Use Macros Easily

article_img
How Do I Create a Macro in Excel- Quick StepsOpen MS Excel > Start Recording a MacroPerform the Actions You Want to Automate > Stop RecordingRun the MacroAre repetitive tasks consuming your time in Excel? With this Excel macros tutorial, you can learn how to create and use macros in Excel to automate tasks and improve efficiency. This guide will walk you through the essentials, from understanding what macros are to creating a macro button for quick access. Whether you're a beginner looking for a step-by-step guide to Excel macros or someone eager to explore advanced features, this article provides everything you need to make and run a macro with ease.How to Create a Macro in ExcelTable of ContentWhat is Macro in Excel Uses of Macros in ExcelHow to Create a Macro in ExcelHow to Run a Macro in ExcelHow to Create a Macro button in ExcelEasy Tips for Creating a Macro What is Macro in Excel An Excel Macros is a set of instructions that you can create to automate repetitive tasks. Think of it like a recorded script that performs actions such as formatting cells, entering data, or generating reports, all with just a single click. This helps save time and reduces the chance of errors
Read More

How to Run a Macro Automatically

article_img
To run a macro automatically when the workbook opens one must enable the developer's tools in Microsoft Excel. What is a Macro in Excel Macros allows automating repetitive tasks in Microsoft Excel. Some tasks need to be performed multiple times and repetition of all the actions would consume time. In such type of Scenarios, Macros helps in automating these tasks. How to Enable the Developer's ToolStep 1: Go to File > Options > Customize Ribbon Step 2: Then checkmark the Developer's tool option in the customize ribbon option. Now you can record Macro, run Macro and do everything that a developer can do. How to Run Macro Automatically when the workbook opensStep 1: Go to the developer's menu and then go to the visual basic Step 2: Go to ThisWorkbook Tab Step 3: Write down Private Sub Workbook_open() and hit enter. You will then see the below screen. You can write your code or basically whatever you want between this and it will automatically run every time the workbook is opened. Let us make a Welcome Message Box: Step 4: Save the workbook as Excel Macro-Enabled Workbook. Note: If you don't save it as an Excel macro-enabled workbook then it will not save the macros and it will
Read More

What Does the Number in the Addchart2 VBA Macro represent?

A chart or graph can be made in an Excel spreadsheet by using the AddChart2 VBA macro. The desired chart type or style is indicated by the number in the AddChart2 VBA macro. The AddChart2 function uses this number as an input parameter to identify the type of chart or style to be constructed. Each chart type is given a different number. If change the argument 2 value, an area chart is represented by the number 1, a line chart by the number 3, and a pie chart by the number 5, similarly, formatting of charts can also be done if change the argument 1 value, data labels are added on the chart, by the number 202, dashed formatted chart is developed by the number 203. You can design a chart that displays your data in the most useful way for your purposes by providing the correct chart type. Data types Before moving forward, we need to learn about the various numerical data types, such as integers, decimals, or floating-point numbers, that may be part of the macro. The numbers in Addchart2 can be of any of the below-mentioned data types, hence it is necessary to know about them. 1. Integer: Using this data format, entire values can be represented between -32,768 and 32,767. 2. Long: Th
Read More

VBA Objects in Excel

article_img
Objects are created from the class. If we take some real-life examples then let's say a Dog is a class where it has its properties and methods like breed, age, and color whereas we can say Dog1 or Dog2 are objects having different breeds, ages, and colors. We can simply say the object is a grouping of data and functions. In VBA "Collection" is a class where we can create its object using "New" so that we can use its in-built methods like Add, Remove and Count. Object ComponentsProperties It is used to read or write a value where we can read the value from the object or we can write a value to the object but we can't update the value in the object Methods Some actions are performed by this method in the object collection is an object where we can perform many actions with the help of methods like Add, Count, and Remove. The following code is shown to add a value in the collection object using the "Add" method Events When an event occurs a function executes called events in the object. Events are like clicking a button, opening a workbook, or activating a worksheet. The following code is shown to display a message by "MsgBox" after an event Workbook_Open() takes place Creating a
Read More

Application Objects in Excel VBA

article_img
The Excel VBA Application object is one of the most commonly utilized objects when using VBA to automate any task. It uses several Excel programs and runs various operations on Excel Workbooks. To work with the Excel Application Object, it has many Properties and Methods. Below is a brief description of each of the most useful Properties and Methods when automating any task. VBA Application Object Methods in ExcelCalculate Method In VBA, the Calculate Application Method is used to calculate all open Workbooks, a single Worksheet, or a range on a worksheet. Syntax In VBA, the Calculate method of the application object has the following syntax: Application.Calculate Example: The macro above is used to perform calculations in a specified Worksheet. CalculateFull Method In VBA, the CalculateFull Application Method is used to force a full calculation in all open Workbooks data. Syntax: Application.CalculateFull Example: The macro above is used to force full calculations in all open workbooks data. FindFile Method In VBA, the FindFile Application Method is used to display the Open dialog box, which allows the user to open a workbook or file. This method returns either True or Fals
Read More

Editing Excel Macros in Excel

article_img
Excel Macro is a set of actions that can be recorded, saved, used multiple times. This feature saves us a lot of time when dealing with repetitive tasks and huge data sets. We can always make changes to an existing Macro in Excel. There are two ways to record and run macros: Excel Commands Excel Visual Basic for Applications(VBA).1. Copying a Macro Code We can reuse the existing macro code while creating new Macros. For example, if you have existing macros created using the record function, we can reuse the code to write new macros. To open the code follow the below steps: Select Tools --> Macros --> Visual Basic Editor. Copy code from the existing macros(use Command + C) and paste(use Command + V) in the new macro2. Renaming a Macro Follow the below steps to rename a Macro in Excel: Select Tools --> Macros --> Visual Basic Editor. Select name on the left plane.Edit the name.3. Deleting a Macro Follow the below steps to delete a macro in Excel: Select Tools --> Macros --> Macros... Select the macro to be deleted. Click on the minus(-) symbol at the bottom.Select yes to delete the macro.
Read More
three90RightbarBannerImg