Excel Automation (Table of Contents)
Introduction to Automation in Excel
Automation in Excel generally involves coding in VBA (Visual Basic for Applications), which is a variation of Visual Basic Language to integrate with Microsoft Office applications. It is an Excel-based programming language that helps the user to automate tasks by writing or recording macros. A macro is a code that runs in an Excel environment and is used to automate repetitive tasks, i.e. if a set of actions has to be repeated several times, then Excel can record these actions and generate a macro containing code to repeat those steps. After recording the macro, the set of actions can be repeated any number of times by running the recorded macro.VBA code produced by macro is placed into a module that can be viewed via VBE.
Some Automation Add-Ins like XLTools, AutoMacro are used to automate one’s own routine Excel tasks without macros.
Examples of Excel Automation
Let us see below how a recorded macro can be used to automate a daily report.
Example #1 – Automation via Macro Recording
Let us say we have a dataset of some numbers, and we wish to have row-wise summary statistics (like sum, average, minimum, maximum) for these. Also, we wish to apply some formatting style to the dataset. So we record these steps in a macro so that whenever we have a new dataset that requires the same operations to be done on it, then this recorded macro can just be run to accomplish this task.
Let’s see how the dataset looks like:
Now to do the required operations and record it in a macro, we will follow the below process:
- Go to ‘Developer’, and click on ‘Record Macro’ :
- On doing this, a pop-up window opens as follows.
- Give the macro an appropriate name and select ‘Personal Macro Workbook’ under the dropdown of ‘Store macro In’, and then click on OK.
- Now we can perform the required operations on the dataset as follows.
- After using formula the result shown below.
- Applying Average Formula in cell H2, the result is shown below.
- Applying MIN Formula in cell I2, the result is shown below.
- Applying MAX formula in cell J2, the result is shown below.
- Now drag these to get these statistics for all the rows.
- Now when all these operations are performed, we stop recording the macro as below.
Note: It is advisable to use keyboard keys while recording a macro. A mouse should only be used when necessary.
4.9 (1,937 ratings)
Example #2 – Using the Recorded Macros
Now let’s say we have another similar dataset like this in ‘Example #2’ of the Excel file which requires the same operations and formatting. So to do this, we just run the above-recorded macro, and our task will be accomplished.
- Go to ‘Developer’ and click on ‘Macros’.
- Now select the macro: “Marks_Macro1’ and click on Run.
- Now on clicking Run, the dataset will be as follows.
So we can see in the above screenshot that on running the recorded macro named as ‘Marks_macro1’, we have automated and hence replicated the operations done on Sheet1 in Example1 to Sheet2. This is automation via recording a macro.
This kind of automation can be used for simple repetitive operations such as daily formatting of a report, communicating with databases, merge files, create new documents, manipulate data, etc.
How Macros are Saved as VBA Code Procedures in VBE
- The Visual Basic Editor has to be accessed by pressing Alt+F11 to open the Visual Basic Editor window.
- On doing this, a window opens. Now under the ‘Project-VBAProject’ pane, we will have the macro stored in one of the modules as follows.
- With VBA Macro Recorder, we do not need to code the macro, instead just record it.
- The macros can be stored in a personal workbook that is a hidden workbook that opens in the background whenever Excel is started. Saving the macros in a Personal workbook makes the macro always available since the Personal Workbook is not system or file specific.
- When recording macros, Excel always produces a sub-procedure (not function procedure). So, recorded codes are only useful for simple macros. More complex macros can be generated by writing VBA codes by enabling the “Developer’ menu in Excel.
- Recording macros to automate tasks can have some limitations. It may not always be possible to record macros that work exactly as we wish. The macro code often requires some manual updates, so in that case, AutoMacro can be used, which is an Add-In that directly installs into VBE. It requires very little knowledge of VBA coding.
Things to Remember About Excel Automation
- Some of the tools used for Excel automation without any coding knowledge are Power Query, VBA Macro Recorder, and some of the Automation Add-Ins like Auto Macro, XLTools. XLTools is an automation Add-In that can even integrate Excel with other applications and tools like SQL, Python. It can be used to write commands in simple Excel tables.
- Macros can even be added to the Excel function menu with a button so that they are available to the user just like Excel built-in functions.
- Recording a macro is helpful to perform simple repetitive tasks and also when we are writing complex macro codes by editing the recorded codes.
This is a guide to Excel Automation. Here we discuss How to Automate Tasks by Writing or Recording Macros along with practical examples and downloadable excel template. You can also go through our other suggested articles –