From the course: Excel: Learning VBA

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Manage variable scope

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…

Contents