only 1 time declaring variables

S

Swingleft

Hello,

question on how to declare variables.

I have something simples like this

Sub Test ()

Dim H1 As Integer

H1 = Range("B6").Value

MsgBox ("Test number: " & H1 )

End Sub

Is there an easy way to use the "H1" value without again having to declare
it?

Sub Test2 ()

MsgBox ("Test 2 number : " & H1 )

End Sub
 
C

Claus Busch

Hi,

Am Fri, 12 Aug 2011 20:45:54 +0200 schrieb Swingleft:
Is there an easy way to use the "H1" value without again having to declare
it?

you have to declare H1 as Public:

Option Explicit
Public H1 As Integer

Sub Test()
H1 = Range("B6").Value
MsgBox ("Test number: " & H1)
End Sub

Sub Test2()
MsgBox ("Test 2 number : " & H1)
End Sub


Regards
Claus Busch
 
S

Swingleft

Thanks...

"Claus Busch" schreef in bericht
Hi,

Am Fri, 12 Aug 2011 20:45:54 +0200 schrieb Swingleft:
Is there an easy way to use the "H1" value without again having to declare
it?

you have to declare H1 as Public:

Option Explicit
Public H1 As Integer

Sub Test()
H1 = Range("B6").Value
MsgBox ("Test number: " & H1)
End Sub

Sub Test2()
MsgBox ("Test 2 number : " & H1)
End Sub


Regards
Claus Busch
 
C

Claus Busch

Hi,

Am Fri, 12 Aug 2011 20:52:42 +0200 schrieb Claus Busch:
Option Explicit
Public H1 As Integer

but you have to run Test() first to initialize H1 or you have to
initialize H1 in Test2() too.


Regards
Claus Busch
 
S

Swingleft

"Mike S" schreef in bericht
Hello,
question on how to declare variables.
I have something simples like this
Sub Test ()
Dim H1 As Integer
H1 = Range("B6").Value
MsgBox ("Test number: " & H1 )
End Sub
Is there an easy way to use the "H1" value without again having to
declare it?
Sub Test2 ()
MsgBox ("Test 2 number : " & H1 )
End Sub

Variable scope:

http://support.microsoft.com/kb/141693
http://www.ozgrid.com/VBA/variable-scope-lifetime.htm

Thanks Mike.. these sites are very usefull
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top