0% found this document useful (0 votes)
13 views7 pages

Macro

Uploaded by

savitamudhal1982
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Macro

Uploaded by

savitamudhal1982
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Class:10

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.

Creating a Simple Macro:


Follow these steps to record a simple macro:
• Click on the View > Macros Record Macro.
• The Record Macro dialog box opens.
• Type a name for the macro, for example, KIPS in Macro name field. By default, Excel gives
the name Macro1.
• Assign a shortcut key Ctrl + key field. This key combination will be use to execute the
macro later.
In the Store macro in list box, choose where the macro is to be made functional. By default,
the macro works in the current worth.
You can also provide some description related to the macro in the Description box. This is
optional.
• Click on OK to start recording the Macro.
• Perform the operations that you want to record in the macro .
• Click on View > Macros > Stop Recording «
recording of the macro.

Creating a Simple Macro:


• Press the keyboard shortcut (here. Ctrl +Shift + K)
to execute the Macro or
• Click on the View > Macros > View Macros. Select the name of the macro that is to be
executed and
click on
• Observe that the operations recorded in the macro get automatically performed.

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).

Using Macro as a Function:


• A function is a line of code that gets executed on function calling.
• When you call a function, it gets invoked and returns result as per the code.
To define a macro as a function, use the keyword Function.
● Each function has a name and may have parameters whose Record values you pass
when you call the function.
To type and execute the above code, follow these steps:
* Switch to the Visual Basic Editor by pressing the Alt + F11 key combination.
* Create a new module (right-click on the Project - VBA Project pane and select Insert ›
Module).
= Type the given code, and save it,
- Again, press the Alt + F11 to switch back to the Excel window and enter the created macro
function in a cell as =Total(). This is referred to as function calling.
• You will get the output as 30 in the same cell.
Syntax:

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.

Accessing Cells Directly Using VBA:

● 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.

You might also like