VBA is a programming language that was created by Microsoft to help program inside their office applications. These office applications have helped students, teachers, office workers, and many others complete tasks quickly. However, users will often find themselves doing redundant tasks or wishing there was a tool to do a unique task. VBA is the tool to use in those situations, you can program time-saving programs to remove redundancy from your work 1.
It may be hard to grasp what kind of projects you can do with VBA. Below are some automation tools that I have built in the past which may help show VBA’s capabilities:
Generating Reports with Data. A common application is to use data from other portions of the code to create a report. Considering Word and Excel are both greatly used in industry, it is common for them to be used to make reports manually. If you use VBA, you can program the report to be generated automatically. This can save you time and reduce the chance of human error. I have built mulitple reports where the data was being extracted from another file and then being populated into a formal report. You will learn more about generating reports in a later VBA blog post.
Converting multiple Word Documents to PDF. A School teacher I know needed to convert a bunch of separate word documents into individual pdf files. The quickest non-VBA way (I know of) to do this is to open each document and export it to pdf. Using VBA this task takes a few seconds.
Window Forms can be used to create UIs. If you need a complex system you can create full-scale Windows Forms (similar to C# Windows Forms). I have used a Windows Form to help “mail merge” information inside a Word Document template. More specifically, most of the reports will be very similar for each client, but you may need to replace the report date, customer name, and repopulate a Bill of Materials table for each customer.
Below is a list of the office applications you can use VBA for 1:
- Excel
- Word
- Outlook
- PowerPoint
- Publisher
- Visio
- Project
- Office for Mac
- Access
Getting Started
This blog series assumes you have an understanding of the basic concepts of programming. Concepts such as variables, conditions, loops, functions, and classes will be used in this series. This blog series will begin with the basics of VBA. Later in the series, the blog posts will focus on creating helpful tools for students and office employees. For this introduction tutorial, Microsoft Word will be used.
Macro Enabled Files
An important thing to remember is that “Macros” also known as scripts or programs, need to be enabled by the user for security reasons. You should always be mindful of the macros you enable and allow to run on your computer. Only enable macros you trust. To be able to run VBA programs, you must have a macro-enabled version of the file. To get a macro-enabled file select it from the save as dropdown. Some examples of the regular file extensions vs. their macro-enabled file extensions are listed below:
- Excel Workbook (.xlsx) vs. Excel Macro-Enabled Workbook (.xlsm)
- Word Document (.docx) vs. Word Macro-Enabled Document (.docm)
Using the Built-In Code Editor
First and foremost is to get the Developer tab on the ribbon. On the ribbon of any of the Microsoft applications, you can edit which tabs are available to you. To add the Developer tab do the following:
- In a new document, go to File.
- Select Options.
- Select Customize Ribbon (On the sidebar).
- Select Main Tabs from the dropdown.
- Select developer.
- Click Add.
- Click Okay.
You can do lots of programming from the developer tab. Consider it your home base for coding inside a Microsoft application. Click the Visual Basic button to start coding.
Once you’ve clicked the Visual Basic button, a new window should appear. Here are some additional steps to get started inside the code editor.
Click Insert and select Module.
Go to the newly created Module.
Use the following code:
Sub Main()
Debug.Print ("Hello World!") ' This will be visible in the Immediate Window
MsgBox ("Hello World!") ' This will make a pop-up window.
End Sub
- Press the green run arrow or F5 to run the code.
Running the Code from a Button
The Visual Basic editor has the green run arrow which works well. However, it would be nice to just have a button you can press once you are done programming. This way you don’t need to go to the developer tab, open up Visual Basic Code Editor, and then hit run.
Making a Button:
Ensure your code is wrapped inside a Sub statement like the above code.
Go to the developer tab on the Ribbon.
Inside the Controls Category, Enable Design Mode by clicking “Design Mode” (Buttons can only be edited inside design mode).
Inside the Controls Category, click the Legacy Tools dropdown and select a “Command Button (ActiveX Control)”.
A simple button should appear in the Word Document. Right-click the button.
View Code.
Inside the Sub statement that appears, call the Main sub from Module 1.
Private Sub CommandButton1_Click()
Module1.Main
End Sub
Disable Design Mode (Button editing is now disabled).
Click the button and see the program run!
Conclusion
At this point, you should know what VBA is and how to start programming inside the Microsoft application of your choice. Additionally, you should have some basic knowledge of creating buttons to run the macros more cleanly. VBA syntax will be covered in the next post.
References
Microsoft Corporation, “Getting started with VBA in Office”, 06/08/2022, https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office ↩︎ ↩︎