TOP

How to insert a macro in Excel

Description

At work, we sometimes lack the standard features of Excel and have to strain our brains to write our own function or find a ready-made macro to solve this problem. This begs the question: What is a macro, how to write and where to insert it?

A macro is a special program written in Excel programming language VBA (Visual Basic for Applications). This language was developed by Microsoft, specifically for MS Office applications.

How to write macros is a separate big topic, on which hundreds of books have been written and there are many sites, so we will not consider it in this article. For those who are interested in this topic in more detail, there is on our website Lessons VBA , where you can learn more about macros.

Let's, after all, analyze where the macros need to be inserted so that they work in Excel.


How to add VBA code?

Suppose we have ready macro to merge cells together with text (recall that by default Excel leaves only the text of the uppermost left cell when combining cells). Now how do we insert the VBA code of our function or procedure?

To add the macro code, you need to open the Visual Basic editor (Alt+F11), right-click on Microsoft Excel Objects, add the VBA module by selecting Insert - Module:

Now, copy the code of our macro there:

Click save and return to Excel.

How to run a macro?

In order to execute a macro, you need to highlight the necessary cells and select the name of our macro in the menu View - Macros (View - Macros) (Alt+F8):

Press the Execute button (Run) and get the result we need:

It's not that difficult...

Related Articles: