variable scope problems

GentleGeek

New Member
Joined
May 31, 2009
Messages
8
I have "variable scope problems".

I have read the help, and I think I understand it, and I have my code organized so that I should have no scope problems.. But I do.

Mostly, my problem seems to occur when I have a Function or Subroutine procedure which calls another Function or Subroutine. Variables which are declared in the Outer Sub are not available to the inner Sub. "That's not right!!" They should be available for the entire scope of the Outer Sub.

Here is an example:
Code:
Option Explicit

Sub Outer()
  Dim MyVar1 As Single, MyVar2 As Single, MyVar3 As Single, MyVar4 As Single
  Inner1
  Inner2
End Sub

Sub Inner1()
  MyVar1 = MyVar1 + MyVar2
End Sub

Sub Inner2()
  MyVar3 = MyVar3 + MyVar4
End Sub
The VBA compiler pops up a Message Box saying- "Compile Error Variable Not Defined". When I move the Message Box, I see that MyVar1 in "Sub Inner1()" is highlighted (blue background, white text). When I "Esc" out of the Message Box, I see that "Sub Inner1()" is highlighted in yellow.

What's wrong with this picture?

Thanks for all help!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
1) You need to declare the variables outside the subs

Code:
Option Explicit

Dim MyVar1 As Single, MyVar2 As Single, MyVar3 As Single, MyVar4 As Single

Sub Outer()
  Inner1
  Inner2
End Sub

Sub Inner1()
  MyVar1 = MyVar1 + MyVar2
End Sub

Sub Inner2()
  MyVar3 = MyVar3 + MyVar4
End Sub

2) You haven't assigned values to the variables.
 
Upvote 0
No it is right.

If you declare a variable within a sub or function it's scope is only within that sub/function.

You've only declared variables in one sub.

In the other's you are trying to use undeclared variables, and since you, wisely, have Option Explicit the code won't compile.

Without Option Explicit the code would compile but the undeclared variables would have no value/be null/nada/nothing/mean hee-haw.:)
 
Upvote 0
Thanks Peter and Norie.

I expected that I could/should do that.. BUT, the Excel VB help says I don't have to:

A procedure-level variable declared with the Dim statement retains a value until the procedure is finished running. If the procedure calls other procedures, the variable retains its value while those procedures are running as well.
Do you guys read that quote any differently? (I know, it doesn't say anything explicitly about declaring variables for the inner function(s), but it surely says that the inner functions will have the variables to work with.)

And Peter-- LOL-- yes, I know.. I don't have any output in my sample code either. I have edited my original post so it now has input and outputs.. My "real" code is much longer, and actually does some real work.


Option Explicit

Sub Outer()
Dim MyVar1 As Single, MyVar2 As Single, MyVar3 As Single, MyVar4 As Single
MyVar1 = 1
MyVar2 = 2
MyVar3 = 3
MyVar4 = 4
Inner1
Inner2
MsgBox("MyVar1= " & MyVar1 & "MyVar2= " & MyVar2 & "MyVar3= " & MyVar3 & _
"MyVar4= " & MyVar4 )
End Sub

Sub Inner1()
MyVar1 = MyVar1 + MyVar2
End Sub

Sub Inner2()
MyVar3 = MyVar3 + MyVar4
End Sub</pre>

Thanks again :),
GG
 
Last edited:
Upvote 0
A procedure level variable is one declared within a Sub - it is only available within that sub (unless you pass it as an argument to another Sub).

A Module level variable (declared at the top of the module outside any Subs) is available to any Sub within that Module.

To make a variable available in any module within a project declare it as Public

Public MyVar1 as Single
 
Upvote 0

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top