I believe that many of the previous tutorials have taught you a lot. In today’s article. The author will teach you something new. That is how to record macro and view macro code in Excel.
Excel provides an automatic code generation function, which is to record macros. The recording macro function can record the user’s interaction with Excel in the form of VBA code. This process is automatic and does not require the user to write code. Excel automatically generates VBA code.
Therefore, use the recording macro function to get how the Excel specified function is realized with VBA. In the actual Excel development process, this method is used frequently. When you don’t know how to write VBA code, just record it, open the editor, and view the code.
Step 1 Prepare a macro-enabled workbook
Excel VBA development requires the workbook to enable macros. Click to get detailed steps.
Step 2 Fill in the Content of the Macro to Be Recorded
The first step in recording a macro is to fill in the information of the macro, including the name of the macro, assign shortcut keys, and save location.
Click the [Developer]→[Record Macro]
In the pop-up recording macro dialog box, you can fill in the macro information.
Step 3 Recording Operation
After filling in the information in the previous step, click OK, and then Excel enters the recording state, and the original recording macro button is updated to “Stop Recording”.
At this time, we can carry out normal operations. Select cell A1 and set the background color to bule.
After completing this simple operation, do not perform other operations, go to the Developer Tools tab, and click the “Stop Recording” button.
Step 4 View the Recorded VBA Code
Alt + F11 to open the VBA editor. As you can see, there is a new module in the left project window.
After finishing recording, Excel automatically inserts a new module with the default name and places the recorded macro code in the module. Double-click the module, you can see the VBA code of the macro just recorded in the code editing window on the right.
We know that a macro is a VBA process. When recording a macro, the name of the final VBA process is the macro name filled in in the second step.
The physical part of the VBA process is the VBA code of the previous operation. In this example, it is the VBA code to set the background color of the cell.
The above content is the basic way to record a macro and view its code. It is recommended that you open an Excel workbook to try, record your frequently used functions and operations, and take a look at these codes. This method is often used in the VBA development process.