How to VBA - Lesson 1: How do you VBA?
Tue Aug 22, 2017 11:11 pm
Hello out there, this will be my first attempt at forming what I hope would be a general but in-depth guide on how to do some programming with PowerPoint!
In this first lesson, we'll go over what is VBA, and where to go to make programming magic happen, and how to make a button run a macro.
So, what is VBA?
VBA stands for Visual Basic for Applications and is the programming language of PowerPoint, Excel and other Microsoft Office programs. VBA programming code tasks are commonly referred to as "macros."
To VBA or not to VBA?
You are going to have to ask yourself this very important question: "Do I really need what I want to accomplish as a VBA macro, or is there a simpler way such as hyperlinks and animations?
The reason you should ask this question, as not everything you do from now on has to be programmed. Don't forget what PowerPoint can do already for you, without you telling it how to do something it already knows how.
Allowing VBA to run
Does your PowerPoint settings allow you to run macros? If not, check out your Trust Center settings in PowerPoint options, and then go to the Macro Settings.
In this first lesson, we'll go over what is VBA, and where to go to make programming magic happen, and how to make a button run a macro.
So, what is VBA?
VBA stands for Visual Basic for Applications and is the programming language of PowerPoint, Excel and other Microsoft Office programs. VBA programming code tasks are commonly referred to as "macros."
To VBA or not to VBA?
You are going to have to ask yourself this very important question: "Do I really need what I want to accomplish as a VBA macro, or is there a simpler way such as hyperlinks and animations?
The reason you should ask this question, as not everything you do from now on has to be programmed. Don't forget what PowerPoint can do already for you, without you telling it how to do something it already knows how.
Allowing VBA to run
Does your PowerPoint settings allow you to run macros? If not, check out your Trust Center settings in PowerPoint options, and then go to the Macro Settings.
!! CAUTION !! |
Don't be so quick as to allow ALL macros to run without your knowledge! VBA is a very powerful language that not only affects PowerPoint but all Microsoft products including the Windows OS and unfortunately, there are some not-so-nice people out there that use macros to do some nasty stuff with VBA. Open all macro-enabled files at your own risk. |
Add Developer Tab to ribbon
Right click anywhere in the ribbon at the top, and select Customize Ribbon. From there, you'll see two columns of tools and the ribbons currently loaded onto your PowerPoint. By default, the Developer tab is checked off. If you check the box to add it and then click OK you will now be able to get to the VBA editor.
Here are some of the tools you get on the Developer tab:
Click on "Visual Basic" to open the VBA edit window.
Click on "Macros" to select and run a macro to perform a task.
Click on Macro Security as a quick shortcut to your Trust Center Settings for Macros.
You can manage Add-ins to run with your file.
You can add ActiveX form controls to PowerPoint and make your own form.
Learning the basics of VBA
Learning VBA is a lot like learning a foreign language because VBA IS a foreign language. You would need to learn proper syntax so that way you don't confuse VBA and cause it to error out. VBA, just like any programming language, is VERY literal and can and will error out easier than you think!
The macros you make are as smart as you code, it will not assume that "Well, looks like he forgot a comma here, so I'll treat this case as if he added a comma and I'll continue on." It will see a missing comma and stop with something like "Syntax error! Debug?" And then tell you "Hey, there's something weird with this line, I don't understand what you want me to do here." Luckily for you, the VBA editor does its best to try to keep you in check with some contextual pop ups as you start typing your code.
The VBA Editor Window
A quick overview of what you're looking at when the window pops up. At the top are your common tools. You'll see a Play, Pause, and Stop button which are used to run a macro from the editor window. Design mode is for making a fancy pop up form which I'll go over in another lesson.
You'll see a pane on the left side which will be your Project explorer window. You will see all PowerPoint files you have open, and their VBA code if they have any. To the right is where you'll see the code in the code window. The code window defaults to maximize the window, but you can make it a resizable window in this space so you can see other windows of code at the same time.
Back to the project explorer, right click on the name of your PowerPoint file, click on Insert, Module. The Module is where you will be storing all your code for now. It's a nice central location in your file for your code to be located. You might see your first line of code at the top for you: "Option Explicit" All this means, is all variables you want to use have to be declared. For example, I can't just say x = 1, I have to tell it "Hey, I'm going to use a variable of 'x' and it's going to be a number." I'll explain more of this later on.
Ok, that's enough background, let's start coding already!
Well now, aren't we a bit hasty? So you got your VBA Editor window open, module created, and ready to type in code. Let's do this, you are going to write your very first macro!
To start a new macro, you begin with giving a name to your macro, also known as subroutine (sub):
- Code:
Sub MyFirstMacro()
This will automatically add the closing tag of:
- Code:
End Sub
Everything you put in between these two lines will run in order from top to bottom and then stop with End Sub.
Note how the words "Sub" and "End" are in blue? Those are reserved words in VBA, and you can't use them as variables on their own. You'll see more reserved words pop up the more you code.
Now, let's create a little pop-up notification to appear with your macro. Add this line in between your Sub and End Sub lines:
- Code:
MsgBox ("If you can read me, my first macro worked!")
If you started typing out "MsgBox" you'll see a contextual note that will guide you on how to properly use this command, with different command settings separated by commas. It will have a section bolded that will tell you which part of the command you are in.
Adding another macro
Start by typing "Sub SecondMacroName()" underneath the End Sub line, and watch what happens when you hit enter. You will see a horizontal break line separating your first macro with your second one you just started. VBA will have these lines to help visually tell you where each macro starts and stops. At the top right side of your code window is a pulldown with all of your subroutines you have created. You can select the subroutine you want to view and it will take you to the top of that subroutine. Neat.
Running your macro
To test this, have your cursor anywhere in between your two Sub lines and click on the Play button at the top.
You can also go back to your PowerPoint window and click on Macros from your Developer tab, and run "MyFirstMacro" and see what happens.
Assign your macro to a shape
Make a shape and place it anywhere on your slide. Give it some text so that we know that this is your macro button. Then, you'll want to go to Action Settings, under the Insert tab at the top. This is where you can tell what happens to the shape when you click it OR mouse over. You can tell it to go to a specific slide OR run a macro. We want it to run a macro.
Note It can also play a sound effect as well (.wav files only) Fortunately, when you tell it to play one of your wav files, it will stay embedded into your PowerPoint until no other objects are using that sound.
After you have assigned your macro to that shape, you can test it by running it in slideshow mode. Click on your button and watch what happens. You just made macro magic!
There you have it! The absolute basics of VBA in PowerPoint. Stay tuned for more VBA fun from yours truly, rusnakcreative.
Re: How to VBA - Lesson 1: How do you VBA?
Wed Dec 20, 2017 4:28 pm
So I have past projects that didn't use "Option Explicit". Is it worth the effort to rewrite the VBA using "Option Explicit"? Or would you say it's just something recommended to use going forward?
(This is a follow-up to your reply from my Bingo Master Board thread.)
(This is a follow-up to your reply from my Bingo Master Board thread.)
Re: How to VBA - Lesson 1: How do you VBA?
Wed Dec 20, 2017 5:35 pm
TimsSlideshowGames wrote:So I have past projects that didn't use "Option Explicit". Is it worth the effort to rewrite the VBA using "Option Explicit"? Or would you say it's just something recommended to use going forward?
(This is a follow-up to your reply from my Bingo Master Board thread.)
To be honest, I really haven't noticed any difference with or without. I'd say it'd be a good habit to leave in, as it only works when a variable is classified/dim'ed. If you leave option explicit and use a random variable you didn't classify, it will error on that line with unknown variable. If you have an error handler to skip over it, every mention of said variable will error and not work as you'd like.
So, moral of the story is to stay on top of your variables, and also their data types! Don't try to put text into a variable that is set to integer, it's not gonna happen. I'll cover more on variables and variable types in the future, before I get ahead of myself!
Re: How to VBA - Lesson 1: How do you VBA?
Sun Aug 26, 2018 4:16 pm
Honestly, I've gotten to the point where if i forget to put Option Explicit in at the top of my VBA code, I'm mad at myself.
I forgot it one day, and couldn't figure out why a trivia text box kept popping up on the wrong slide... I had a global variable and a local variables named the same... guess which one got precedence? The one I didn't want to use.
I forgot it one day, and couldn't figure out why a trivia text box kept popping up on the wrong slide... I had a global variable and a local variables named the same... guess which one got precedence? The one I didn't want to use.
- JoeyTrittenAspiring PPT Creator
- Posts : 13
Join date : 2017-09-19
Location : Forgot
Re: How to VBA - Lesson 1: How do you VBA?
Fri Nov 30, 2018 8:01 am
Hi there Runsak. I was wondering How I would make a type-in cheat code section for a project I am working on.
What I want to happen is that I have a text box control that if you type in a certain word (Let's say for instance "ABC") and click the command button bellow it will take you to a certain slide. Any way I could do this?
What I want to happen is that I have a text box control that if you type in a certain word (Let's say for instance "ABC") and click the command button bellow it will take you to a certain slide. Any way I could do this?
Permissions in this forum:
You cannot reply to topics in this forum