Write the First VBA Macro by Yourself

Date:2020-11-11 Author:Sandra

Through the previous tutorials, we have a basic understanding of Excel VBA, and you maybe have initially learned to use the VBA editor. If you haven’t read it yet, it is recommended to read and study first.

In this tutorial, the author will introduce how to write a VBA macro from scratch, that is, a VBA process. It is recommended to follow the steps to practice by yourself, enter the code manually and run it.

Step1. Create a macro-enabled workbook

You need to create a new workbook and save the workbook as an “Excel Macro-Enabled Workbook(*.xlsm)” type Click to get detailed steps.

Write the First VBA Macro by Yourself

Step2. Open VBA Editor

You can use shortcut Alt+F11 to quickly open Visual Basic. Click to get detailed steps.

Write the First VBA Macro by Yourself

Step3. Insert a New Module

The VBA code in an Excel workbook is usually stored in a worksheet object or module. In this example, we use a module to save the VBA code.

Write the First VBA Macro by Yourself
Write the First VBA Macro by Yourself

After the insertion is completed, the newly inserted module will be displayed in properties window. You can modify the name of the module.

Write the First VBA Macro by Yourself

Step4. Open the Module

Double-click the module 1 inserted in the previous step. In the code window area on the right, the code editor for module 1 will open. The module corresponding to the currently opened code editor in the code window on the right can be seen at the end of the Editor title.

Write the First VBA Macro by Yourself

Step5. Create a Macro

In the code editing window, enter the following code:

Sub MyCode()

End Sub

This code is an empty VBA process with only the beginning and the end. The other code of the process is written in the middle. Among them, Sub is the abbreviation of Subroutine, which represents a process in VBA and also represents the beginning of a process. The next line End Sub indicates the end of the process.

Write the First VBA Macro by Yourself

Step6. Write the VBA Process

The physical part of the process refers to the part that will actually be executed. The previous step created an empty VBA process, which does not contain entity code. Although it can run normally, there will be no output.

In this step we write the physical part of the VBA process. Enter the following code between the beginning and the end of the process.

Msgbox “Hello Excel”

Step7. Run the Code

We have finished writing a complete VBA process, run it now and see the results.

Place the cursor on any part of the code, click the run “▶” button on the toolbar, or press F5 to run the code.

Write the First VBA Macro by Yourself
Write the First VBA Macro by Yourself

You can see the results of the operation, and Excel pops up a dialog box, showing that the content is exactly what is written in the code.

Summary

This tutorial introduces how to write a VBA process from scratch. The steps may seem cumbersome, but in fact the operation is very simple. The author suggests that you open a Excel workbook when you see this tutorial and try it.

Copyright Statement: Regarding all of the posts by this website, any copy or use shall get the written permission or authorization from Myofficetricks.

Leave a Reply

Your email address will not be published. Required fields are marked *