Locked lesson.
About this lesson
Looking at the different keywords and structure that make up a valid macro.
Exercise files
There are no related exercise files for this lesson.
Quick reference
Understanding Code: Macro Syntax
An overview of VBA subroutine syntax.
When to use
To understand the key portions of a subroutine signature and how to read it (so that you can evenutally write your own.)
Key Components of SubRoutine Signature
Example
[Scope] Sub Name([Parameter1],[…])
‘Code goes here
End Sub
Observations
- [Scope] is optional, and will default to "Public" if left blank.
- Sub is short for “subroutine” and always appears at the beginning.
- Name is the name of the macro subroutine.
- The open and close parentheses () are always required
- [Parameters] within the parentheses are optional, and may or may not be present.
- A subroutine always ends with End Sub.
Hints & tips
- Optional items appear in square brackets
- Anything after a single quote (‘) are comments and are not executed at run time
- 00:05 The most important thing that I can teach you in this course is how to read code.
- 00:11 And that's where we're gonna start here, before we get into really mucking
- 00:14 around and playing with it is how to actually read what we see.
- 00:17 If you can read it, you can edit it.
- 00:19 If you can edit it, you'll eventually learn to debug it, and
- 00:22 then you'll be able to write your own cool stuff but
- 00:24 the big key first is always being able to read code.
- 00:28 To start with what I wanna do is I wanna look at the structure of what we call
- 00:33 a Macro Subroutine.
- 00:34 The subroutine is the container that holds all of the code that so
- 00:38 far we've recorded.
- 00:39 And if we ever decide to start running our own macros,
- 00:41 it's gonna hold all of the macro code that we actually use, all the VBA code.
- 00:46 And the structure of a Macro Subroutine always looks like this.
- 00:52 You'll notice that I've tried to keep my syntax similar to the excel formula syntax
- 00:57 here, so optional pieces are in square brackets.
- 01:00 So the first thing that we see is we start with an optional scope there is a default
- 01:04 for this and if you don't declare it, which you will most likely not
- 01:07 see it declared it will just be blank and that's okay.
- 01:11 The next piece is that our key word Sub is in blue,
- 01:15 Sub is something that is always used to do our subroutine.
- 01:19 It's short for subroutine.
- 01:20 This is part of it.
- 01:21 This is part of the signature of our macro.
- 01:23 So let's start with sub.
- 01:25 The next part is the name.
- 01:26 The name is what we chose.
- 01:28 So in the case of our first example, that name would be roll forward.
- 01:31 So it would say subrollforward.
- 01:33 And if you actually look at that code it actually says open and close parenthesis.
- 01:38 The parenthesis are always required.
- 01:41 But the things in between are not.
- 01:42 Parameter1, parameter2, three, four, five,
- 01:44 six however how many one are completely optional.
- 01:47 In our case we don't need them because we aren't feeding any primers in.
- 01:50 That's gonna be a bit more advance will look at that when we get into user-defined
- 01:54 functions in the end of this course.
- 01:56 The next line that we have after our sub roll forward open close parentheses
- 02:01 is a green line.
- 02:02 It has an apostrophe at the beginning of it and it says code goes here.
- 02:06 Green lines with a single quote or
- 02:08 apostrophe in front of them, those are comments.
- 02:11 They don't get executed, they're not interpreted, they're not Run, okay?
- 02:14 So this is what we actually look at and say, green lines, comments, okay cool.
- 02:18 We can just leave notes for ourselves.
- 02:21 You'll have a bunch of code underneath that, which will be in black and
- 02:24 other blue keywords and whatnot.
- 02:25 And then the routine always ends with the line End Sub.
- 02:29 And underneath that we create another one, sub whatever okay.
- 02:34 So this is the overall structure of a container for code.
- 02:40 As far as the scope goes,
- 02:41 the scope can be one of two things.It can either be public or it can be private.
- 02:47 The default scope if you leave it off is public and
- 02:50 what that means is that Anyone can run the routine,
- 02:54 you can hit Alt + F8, it'll show up in the dialog box and away you go.
- 02:58 If you were to declare it as private, you would need to be able to call that
- 03:03 routine somehow, and I'll show you that later on in the stuff as we go through.
- 03:08 But the big key part here would that you
- 03:11 would not be able to see it when you press alt Alt F8.
- 03:13 Now if you had a keyboard shortcut assigned to it, it would still run but
- 03:17 you wouldn't see it in your Alt F8 area.
- 03:19 Okay, so its just hiding it a little bit more.
- 03:22 There's a little bit more complexity to scope than that but for
- 03:24 the most part that's the effect that you're gonna see at this stage with what's
- 03:27 actually ends up happening there.
- 03:30 There's some key things around the name for
- 03:32 your macro that you're gonna wanna know.
- 03:35 You cannot ever start your subroutine with a number.
- 03:39 So it can't say Sub 1 whatever okay.
- 03:44 So it can't say sub one, two, three, four.
- 03:46 It can't say sub one document nothing like that.
- 03:49 You can use a number later in it.
- 03:52 You could have sub document one, or sub macro one.
- 03:55 That's okay, but you can't lead with a number.
- 03:58 You cannot use any special characters except an underscore.
- 04:03 Okay, so you can't use asterisks, you can't use dashes but
- 04:06 you can use an underscore and this is what we actually call the programmer space
- 04:11 because you're also not allowed to use spaces in the name of your macros as well.
- 04:16 So I couldn't have sub roll space forward I could have sub role forward all
- 04:21 mashed together or I could have sub role underscore forward, that would be allowed.
- 04:26 But I can't start with the underscore either.
- 04:27 Okay so something else to be aware of in that particular case.
Lesson notes are only available for subscribers.