Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

trouble using globals

  Asked By: Thelma    Date: Jan 19    Category: MS Office    Views: 585
  

When I try to run some simple code (reproduced below), with a global
variable declaration I get the following errror:

Compile Error:
"Invalid Attribute or Sub in function"
And the word Global is highlighted.
It won't work with Public either, only Dim.

I'm using Excel 2007.
Any idea what I'm doing wrong?

Sample Code:

Sub DoThis()

Global strName As String

strName = Range("A1").Address
DoThat

End Sub

Function DoThat()

MsgBox "Name: " & strName

End Function

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Riley-jack Johnson     Answered On: Jan 19

I've never seen a VBA "Global" dimension statement like you are trying
to use. I just declare the variable outside of a SUB or FUNCTION to
make it global. For example:

Dim strName As String
Sub DoThis()
strName = Range("A1").Address
DoThat
End Sub
Function DoThat()
MsgBox "Name: " & strName
End Function

 
Answer #2    Answered By: Kawthar Malik     Answered On: Jan 19

You can only use Global outside subs and functions.

For the record, you can only use Global in regular Modules, not Class
Modules or UserForms.

 
Answer #3    Answered By: Anthony Smith     Answered On: Jan 19

You can define a "Public" variable in the definitions section above the subs in
a module. It
is then available to all subs and functions.

Like this
Public strName As String

Sub DoWhatever()
...
strName = YourCodeHere
...
End Sub

 
Didn't find what you were looking for? Find more on trouble using globals Or get search suggestion and latest updates.




Tagged: