Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Carrying Variable Value Between Modules

Status
Not open for further replies.

Kevsim

Instructor
Apr 18, 2000
385
AU
In the one Workbook, I have several modules, I wish to carry the variable value obtained from one module into the other module.

If I declare the variable as Public, I receive the following error message, “Invalid attribute in sub or function.”

I would appreciate some assistance in doing this.
kevsim
 
Make sure you are decalring the variable "Public" at the module level and not the sheet event level.


I.E. Insert>New Module...

public my_variable as string

 
ETID,
Thanks for the info, I am still lost, example code is as follows -

Public Sub Test()
Public my_variable As String
my_variable = MsgBox("Testing", vbOKOnly)
End Sub

Still receive the same error message.

Could you please explain a little further.
kevsim
 
The Public declaration needs to go before all other subs and functions in that module sheet:
Code:
Public my_variable As String

Public Sub Test()
my_variable = MsgBox("Testing", vbOKOnly)
End Sub

Sub OtherMacro()
'Your code
MsgBox my_variable
End Sub
 
Thank you for the information, I finally worked it out, all now working OK.
kevsim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top