If you regularly work with word pro­cess­ing programs, database systems, or spread­sheet ap­pli­ca­tions, then you know how tedious it can be to enter the same di­rec­tives over and over again. More than anything, complex, multi-stage program sequences can be a real test of patience. Macros record a sequence of commands so that they can be executed au­to­mat­i­cal­ly by the software user at any given time. Even though macros are written in a pro­gram­ming language, pro­gram­ming knowledge generally isn’t needed, as many programs — including Excel – can record simple macros and provide as­sis­tance in de­vel­op­ment. For the Microsoft spread­sheet software Excel, macros are one of the most important features.

Domain Name Reg­is­tra­tion
Build your brand on a great domain
  • Free Wildcard SSL for safer data transfers 
  • Free private reg­is­tra­tion for more privacy
  • Free Domain Connect for easy DNS setup

What is an Excel macro?

Microsoft Excel is the most sought-after solution for pro­cess­ing, analyzing, and pre­sent­ing data. Using Excel spread­sheets, which forms a key component of the Microsoft Office Suite, Windows sub­scribers have been working with this program for years to create budgets, in­di­vid­ual calendars, and a whole host of other projects. Excel is also used in business: With the software, project plans, hours lists, or budget plans are realized just as easily as graphical rep­re­sen­ta­tions of sales figures, profits, or losses. Anyone familiar with the program learns to ap­pre­ci­ate the various functions but at the same time, most people quickly develop a re­luc­tance to con­stant­ly repeat routine tasks or repet­i­tive actions that can’t easily be performed using the standard user interface.

So it comes as no surprise that the pos­si­bil­i­ty to create macros in Excel is one of the core features of the spread­sheet program. There’s an in­te­grat­ed macro-recording tool for this purpose that uses the Visual Basic for Ap­pli­ca­tion (VBA) script language, which is also used in the Microsoft Office Word, Pow­er­Point, Access, and Outlook programs. Thanks to the language, you can easily create your own Excel macros that au­to­mat­i­cal­ly execute defined routine commands, or add new functions to the spread­sheet cal­cu­la­tion (i.e., al­go­rithms for data analysis).

How macros work in Excel

Excel provides various elements of the user interface in the form of hi­er­ar­chi­cal­ly organized program objects. Each one features specific prop­er­ties and methods. All available objects are related to each other in the hi­er­ar­chi­cal object model and are reflected ap­prox­i­mate­ly in the visual user interface, which provides buttons and input boxes for in­ter­ac­tion with the ap­pli­ca­tion. By setting the prop­er­ties and invoking their methods, you can edit the different objects. For example, a “Close” method exists for the “Workbook” object that can be used to close the selected workbook as well as the “Ac­tiveSheet” property that rep­re­sents the sheet currently active in the workbook.

With the help of col­lec­tions, macros can also perform actions on a whole group of objects. For example, the “Work­sheets” col­lec­tion object causes the state­ments in the macro to apply to all work­sheets. For the execution of a macro, you have the following three options:

  • Selection in the macro menu
  • Click on an in­di­vid­u­al­ly created button
  • In­di­vid­ual key com­bi­na­tions

Benefit of Excel macros

If you pre­vi­ous­ly, either con­scious­ly or un­con­scious­ly, refrained from creating macros in Excel, this doesn’t nec­es­sar­i­ly have a negative effect on your Excel files. But you have chosen a rather tedious way to work. Macro tech­nol­o­gy offers a vast number of decisive ad­van­tages that make it a must for anyone who wants to get the most out of their cal­cu­la­tion software:

  • Macros reduce the error rate: With every directive that you manually enter into Excel, you increase the prob­a­bil­i­ty of an incorrect entry. Es­pe­cial­ly in complex command sequences and fre­quent­ly repeated tasks, mistakes are quickly made that derail the entire func­tion­al­i­ty of the sheets you are working on. In a macro, only the creation process has error potential. If no errors manage to sneak in there, then the Excel macro will operate error-free every time.
  • Macros reduce the work effort: You only create macros one time. You can then start using the practical command com­bi­na­tion as often as you want by pressing the assigned key com­bi­na­tion. It saves you a lot of time that you can now invest in other areas of work.
  • Macros expand Excel: With VBA, it’s not just a case of pro­gram­ming macros that group commands together, you can also develop new functions. This extends the in­te­grat­ed function col­lec­tion of your cus­tomized user-defined workbook and sig­nif­i­cant­ly sim­pli­fies your formulas. Excel also presents your cus­tomized functions as if they were native functions. In addition, you have the option of linking all of the macros that have been created with a button in the toolbar, which can then be used to trigger their functions.

How to create your own macros in Excel

To create your own command col­lec­tion, you first have to activate the VBA editor, which is a part of the developer tools. These are not ac­ces­si­ble on the menu list by default (also called the “ribbon” in Microsoft Office ap­pli­ca­tions), so you need to add the cor­re­spond­ing tab in the first step.

Step 1: Add the “Developer” tab

Select the “file” tab and click on “Options”. Under the “Customize Ribbon” item, find the list of main tabs, which includes the “Developer” tab.

Check the box next to “Developer” and confirm the change by clicking “OK”. The tab will now appear in the menu ribbon.

Step 2: Create a new workbook for macros

The second step is to create a new Excel folder to serve as the basis for the macros that you are about to record. To do this, click on the “Macros” button in the “Developer” tab, enter the name of the folder (in this example, “Hello”) under “Macro name” and confirm by clicking on “Create," at which point the VBA editor will open, including the following code:

Sub here stands for sub­rou­tine and also for a macro, which is in itself a kind of sub­rou­tine for Excel. For the re­spec­tive macro, the entire code between Sub and End Sub is executed. If you would like to conduct a small test, you can extend the code of the sample “Hello” macro as follows:

Sub Hallo()
    MsgBox ("Hello world!")
End Sub

Save the result in the .xslm format, switch back to the Excel program interface, re­ac­ti­vate the “Macros” button, and select the “Hello” macro in the displayed list. Now, if you click on “Run," a small in­for­ma­tion window appears with the text content you just defined.

Click on the “OK” button to close the window and stop running the macro. Then save the macro workbook in the VBA editor.

Step 3: Create a quick-access button for macros

In both the “Developer” tab and the “View” tab, you can display and run the available Excel macros. But if you need the command summary more fre­quent­ly, it would make sense to create a button for quick access to the toolbar. This is done as follows:

  1. Go to the “File” tab.

  2. Open the options, and click on the menu item “Quick-access Toolbar”.

  3. In the “Select command” list, click on the “Macros” entry and search for the pre­vi­ous­ly created “Hello” macro.

  4. Select this and click “Add," then transfer it to the list of quick-access buttons.

  5. Before you confirm the new button via “OK," you can first select the symbol that will be shown later. Simply select the macro and click on “Change”.

Now, next to the standard “Save," “Undo," and “Redo” buttons on the toolbar. There’s also the quick-access button for the “Hello” macro:

Note: You can also set a key com­bi­na­tion for running a macro instead (“Developer” -> “Macros” -> “Options”).

Step 4: Use the macro recording

The VBA editor in­te­grat­ed by Microsoft into its Office ap­pli­ca­tions has the benefit of you not being required to master the pro­gram­ming language in order to create simple Excel macros. For this, the in­te­grat­ed function for recording macros is perfectly adequate. For example, we’ll now create a macro that au­to­mat­i­cal­ly renames a worksheet.

  1. In the developer tools, select the “Record macro” button this time.

  2. In the following dialog window, name the macro “Re­name­Work­sheets” and start the recording with “OK”.

  3. Now rename “Table1” to “New Name” and end the macro recording by clicking on “End recording”.

If you switch back to the VBA editor now (“Developer” -> “Macros” -> “Edit”), the following code should appear:

In the first four code lines under the Sub line, each beginning with an apos­tro­phe, are the com­men­tary lines. These have no influence on the overall func­tion­al­i­ty of the macros and primarily simply offer a better un­der­stand­ing of the code. You can also tem­porar­i­ly de­ac­ti­vate code lines with the help of com­men­tary. For this example macro, you don’t need the four au­to­mat­i­cal­ly created lines, so you can safely delete them.

The next line contains the selection method, selecting “Table1," which was necessary for the manual rename execution before you could give it a new name. VBA scripts don’t need objects to be selected in order to be edited, so these code lines aren’t necessary for macros in Excel. Com­plete­ly revised, the code looks like this:

Sub RenameWorksheets ()
    Sheets("Table1").Name = "New Name"
End Sub

Switch back to Excel and rename the worksheet back to “Table1”. Now run the Re­name­Work­sheets macro as a test to see if the name changes au­to­mat­i­cal­ly. Note that the macro will have to be adapted to the new name if you want to run it again.

Excel macros in practice: Diagrams and input forms

VBA scripts enable the au­toma­tion of various Excel tasks. With the help of the recording option, you will simplify your own work process and also acquire knowledge about the high-per­for­mance pro­gram­ming language. Don’t hesitate to use the VBA tool if you suspect that an Excel macro will make a par­tic­u­lar task easier.

Having already presented the general approach to macro-building in the previous para­graphs, the following two examples will now help by giving you a concise overview of how versatile the command au­toma­tion is.

Excel Evergreen: Create a diagram based on a cell range

A common function of Excel tables is the vi­su­al­iza­tion of input data in the form of a diagram. With this quite often tedious task, a macro can provide valuable as­sis­tance. For this, you’ll first create a macro with the name “As­sort­ed­Tasks” and declare the variable for your diagram object:

Dim mydiagram As Chartobject

In the next line, create a diagram object for the mydiagram variable to refer to:

Set mydiagram = ActiveSheet.ChartObjects.Add(100, 50, 200, 200)

The values that are contained in the brackets define the position as well as the size of the diagram. The first two sets of data are the co­or­di­nates of the upper left corner, followed by the values for the width and the height. If you run the macro now, Excel au­to­mat­i­cal­ly creates the object at the defined position and in the defined size — but it’s obviously still blank since at this point the input data is still missing.

The last step is to fill the diagram object with data, which can then be vi­su­al­ized ac­cord­ing­ly. Inputting the values is required for this, as well as a further ad­just­ment of the Excel macro since it still lacks in­for­ma­tion regarding where the in­for­ma­tion should be displayed. Within the With … End With con­struc­tion, now use the Set­Source­Da­ta method and specify the Selection value, which will cause the values of all cells that are marked when the macro is run to be taken into account. The code of the complete macro looks as follows:

Sub AssortedTasks()
Dim mydiagram As ChartObject
Set mydiagram = ActiveSheet.ChartObjects.Add(100, 50, 200, 200)
With mydiagram
.Chart.SetSourceData Source := Selection
End With
End Sub

For test purposes, give cells A1 through A5 the values 1 through 5 and run the macro to see the data presented to you as a bar graph. This type of diagram is created by default in Excel, as long as you don’t supply ad­di­tion­al ChartType details.

How to create a dialog box for user input

With the help of the macro tool, you can also create an input form through which the user can interact with Excel. For example, to create an in­ter­ac­tion window whose input value is au­to­mat­i­cal­ly written into a defined cell, start by creating and naming the macro as usual. In our example, we give the command col­lec­tion the name “InputForm”. Then you name the sub­rou­tine des­ti­na­tion of the value entered later by the user:

Sub InputForm()
    ActiveSheet.Range("A1").Value = 
End Sub

The macro ensures that the input of the user is trans­ferred to the A1 cell of Table 1. The value of the field (Value) now needs to be assigned in the second part of the statement. We decided on the InputBox command as an example, which is very similar to the MsgBox command used in the “Hello” macro. Combined with the following three arguments, which are enclosed in quotation marks in the VBA code, a suitable input window is created.

  • Prompt: With the help of the first argument, define the text of the dialog window that supports the user during input.
  • Title: The “Title” argument displays the title of the input form.
  • Default: Finally, you can also specify a standard input with “Default”.

A possible example code of the complete “InputForm” macro including the box and text looks as follows:

Sub InputForm()
    Table1.Range("A1").Value = InputBox("Please enter a value for the field A1.", "Title of the input form", "Value for field A1")
End Sub

If you run the following Excel macro, the cor­re­spond­ing dialog window will appear:

Trans­fer­ring Excel macros

If you’ve created macros in your Excel workbook, you can easily use them in another document or transfer them to another user. The VBA editor has an import/export function for this purpose, with which you can save your command col­lec­tion in the .bas format or integrate macros already saved in the .bas format into your current Excel document. The only re­quire­ment is that macros are activated in the target document. Since the handy scripts can contain po­ten­tial­ly unsafe code, Excel has re­stric­tions in place to protect users, so that all macros without a digital signature are blocked, for example, with or without a no­ti­fi­ca­tion. The simple solution is to au­to­mat­i­cal­ly allow all macros and make sure that all codes are trust­wor­thy before importing.

Find the options for ac­ti­vat­ing and de­ac­ti­vat­ing macros is in the “Trust Center” (“Security Center” in older versions): Simply go to the “File” tab, choose “Options," “Trust Center," and click on the “Trust Center Settings” button. There you’ll find the “Macro Settings” menu item, which contains the settings mentioned here.

To export a macro, open the Visual Basic Editor, click on the “File” tab and then on “Export file”. Give your macro col­lec­tion a mean­ing­ful name, choose the desired index, and complete the process by clicking “Save”. The exported file is only a few bytes big and can easily be sent via e-mail or trans­port­ed via a portable storage device to the desired target computer. To import a macro file, proceed the same way initially but then choose “Import file” and specify the save location of the file.

HiDrive Cloud Storage
Store and share your data on the go
  • Store, share, and edit data easily
  • Backed up and highly secure
  • Sync with all devices
Go to Main Menu