~4 Minute Read
vba,
productivity
I am a Mechanical Engineering Technologist currently attending the University of Calgary to further my engineering education. More specifcally, I have interests in mechanical, electrical, and software engineering.
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:
Below is a list of the office applications you can use VBA for 1:
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.
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:
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:
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
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!
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.
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 ↩︎ ↩︎