Tuesday, July 28, 2009

How do I declare a variable Globally so it can be accessed by multiple workbooks in Excel Visual Basic ?

I am trying to pass variable between different workbooks in Excel Visual Basic. I know I have to make it Public but since I want to make this variable to be used by other modules in other workbooks I know I have to set up a reference to it using the Tools command - but it is here where I dont know how to proceed !





Can anyone suggest a way forward ?

How do I declare a variable Globally so it can be accessed by multiple workbooks in Excel Visual Basic ?
Global variables are self contained in each workbook. If there are more than one workbook open at the same time then you can pass the variable through.





at the top of your code type:





Public Const myString = "yourdataVariable"


(if your variable is constant)





Dim myString as String


(if your variable changes)





after you tyep it in and hit enter there will appear a line break. this will show that all var's above the line are global





If worse comes to worse you can always open the workbook were you want to variable passed and put it in a cell and then use another variable to pick up its value
Reply:Create a code module with the variable declared as public. Save it as an Add-In (having an .XLA extension). Use the Add-In manager to select the newly created add-in. It will be available in all future workbooks.

flower delivery

No comments:

Post a Comment