Varialbe Loses Value

  • Thread starter Thread starter William Wolfe
  • Start date Start date
W

William Wolfe

I have a subroutine that calls two other subroutines, all in the same
module. The first subroutine has a varialbe "Column" that is use as a
column index. I also want to use this same variable in one of the
subroutines that the original subroutine calls. When I go to the subroutine
that is called, the variable "Column" is empty. How do I maintain the value
of "Column" between subroutines?
 
The scope of a variable declared within a procedure is local to that
procedure. Other procedures can not see it. To that end however we can pass
variables like this

Sub this()
dim lng as long
lng = 10
call that(lng)
end sub

Sub that(byval lng as long)
msgbox lng
end sub
 
You have most likely declared your variable in the first subroutine?

Sub Whatever()
Dim Column as variant
your code
end sub

Having done that, the variable is only valid inside this routine. Move the
variable declaration right to the top, before your routines, and it will work.
eg
Dim column as Variant
dim whatever as whatever

Sub Whatever()
your code
end sub

--
HTH

Kassie

Replace xxx with hotmail
 
While that will work you are best off to minimize the number of glabal
variables in a project (placing the variable outside the procedure creating a
global). Where it is possible to pass a variable that is the prefered method.
As projects get larger global variables become extremely combersome to manage
and debug.

Additionally I am sure it is not your intention to declare a variable with
the same name as a procedure name...

dim whatever as whatever

Sub Whatever()
 
Back
Top