From the course: Excel: Learning VBA
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Manage variable scope - Microsoft Excel Tutorial
From the course: Excel: Learning VBA
Manage variable scope
- [Instructor] If you have multiple subroutines within a single code module, you can save time by declaring a variable that you use in several subroutines at the top in the declaration section. In this movie, I will show you how to do that. My sample file is 02_03_Scope, and you can find it in the chapter two folder of the exercise files collection. A variable scope refers to which elements of a workbook or other workbooks can see a particular variable. To demonstrate what that looks like, I will switch over to the Visual Basic Editor by pressing alt F11. In module one, I have two subroutines and they basically do the same thing, except that the top calculates a commission by multiplying by a 5% rate and the second has a bonus and it uses a rate of 8%. And you can also see that I have single rate declared within both of the subroutines. I'll run the meet so you can see what happens. So I've clicked in the top one, and I'll press…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Introduce Excel VBA data types4m 5s
-
(Locked)
Declare variables and require declaration before use5m 27s
-
(Locked)
Manage variable scope3m 50s
-
(Locked)
Define static variables and constants5m 42s
-
Create a calculation using mathematical operators5m 10s
-
(Locked)
Define arrays7m 10s
-
Define and use object variables3m 3s
-
(Locked)
Streamline code references using With…End With statements4m 40s
-
(Locked)
Challenge: Define variables, constants, and calculations2m 5s
-
(Locked)
Solution: Define variables, constants, and calculations3m 13s
-
(Locked)
-
-
-
-
-
-