Macro
Macro
Macro
Unit :2( Electronic Spreadsheet)
Introduction:
Let us consider an example, where a bookseller Rajneesh creates a report every month to
view his sales and calculate profit.
• He also formats the names of the customers with overdue accounts in and applies bold
formatting whcreating the report.
• It means that every time he crea he perform all these formatting operation again.
• Introduction:
• 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.
• 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.
Macro Coding:
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 the recording, save the macro, and run it,
Excel simply executes the VBA code it generated and follows exactly the same steps you
performed.
Inshort, a code of macro is written in Visual Basic Editor (VBE), which is a part of Microsoft
Visual Basic for Applications (VBA).
To pass arguments to a macro while calling it in Excel directly by entering the function name
and argument in a cell:
To type and execute the given code, follow these steps:
→ Place a command button on the worksheet. For this, click on the Developer tab
and select Insert › Button (From Control), and drag the button on the
spreadsheet.
→ The Assign Macro dialog box opens. Enter the name for the macro, and click
on New.
→ Type the code as given in the example, and save it by clicking on the Save
button
available on the Standard toolbar.
→ Press Alt + F11 to switch back to Excel, and Click on the command button! It
will invoke the created function and you will get 10 as the output in the message
box.
● In Excel, it is Possible to access cells) directly through VBA. For this, you need to
read the data from the worksheet and based on the data you can perform further
operations.
● The process of fetching the data from the worksheet to VBA is simple. You can fetch
the data using a cell object or range object.
● Cell object: It allows You to refer to a cell by its row number and column number (i.e.,
1,1).
● In cell object, the row number is entered first.
● Image object: It allows you to refer to a cell using the standard "A1" notation.
Let us learn how to write the code to read the data from Excel to VBA using the cell range
objects.
The above code will read the value from the first row and first column, i.e., from the first cell
of the worksheet and display the value in the message box.
Now, consider another example to read the value using the cell object.