Jump to content

Visual Basic for Applications/Introduction to Macros

From Wikiversity

Macros are small programs that record your actions as you perform a task in Excel. When you run the macro later, it repeats your keystrokes, and thus actions. This is why macros are great for automating repetitive tasks. The macros feature of Excel allows you to record a set of actions that you perform repeatedly in a spreadsheet. You can run a macro as many times as you want. They automate the recorded actions and save your time and efforts. Macros are very useful when you have to repeat the same task in the same way, over and over again.

When you create a macro, you are actively recording your mouse clicks and keystrokes. So with a few clicks you can quickly replace the repetitive tasks. Let us consider an example, where a bookseller Huzaifa creates a report every month to view his sales and calculate profits. He also formats the names of the customers with overdue accounts in red, and applies bold formatting while creating the report. It means that every time he creates the monthly report, he has to perform all these formatting of operations, again and again. To avoid such a situation and speed up his task, he can create a macro to record the formatting and run it to apply these formatting changes quickly to the cells, whenever required.

When you record a macro, Excel notes down the steps you are taking in a language that it understands- which is Visual Basic for Applications (VBA). When you stop recording, save the macro, and run it, Excel simply executes the VBA code it generated and follows exactly the same steps you performed. In short, a code of macro is written in Visual Basic Editor (VBE), which is a part of Microsoft Visual Basic for Applications (VBA). You need to download Microsoft Visual Basic for Applications.

Macros enable you to automate almost any task that you want to perform in a spreadsheet. Let us learn how to record a simple macro and how to write a code for creating macros in Excel.

[edit | edit source]