ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Global Variables in Excel VBA (https://www.excelbanter.com/excel-programming/376770-global-variables-excel-vba.html)

[email protected]

Global Variables in Excel VBA
 
I have an excel program, heavily driven by menus and buttons with
various entry-points and exits.

I am however attempting to create a global variable that could be used
to store and access data, regardless of the entry/exit point used.

Use of the public command would create a global variable which would to
this. However, given the fact that a user may come in at any point
(depending on what button or menu they use), there is no obvious way to
create a global variable.

Is there a way to actually create a global variable - that could be
used regardless of what a user presses?


Chip Pearson

Global Variables in Excel VBA
 
I don't completely understand your question. If you declare a Public (the
new term for "Global"), in a standard code module (not in a object module
like a Userform or Class module), you can write/read to and from that
variable from any procedure in any module in the project. E.g.,

Public MyVar As Variant

Beyond that, I don't understand what your dilemma is.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)


wrote in message
ups.com...
I have an excel program, heavily driven by menus and buttons with
various entry-points and exits.

I am however attempting to create a global variable that could be used
to store and access data, regardless of the entry/exit point used.

Use of the public command would create a global variable which would to
this. However, given the fact that a user may come in at any point
(depending on what button or menu they use), there is no obvious way to
create a global variable.

Is there a way to actually create a global variable - that could be
used regardless of what a user presses?




[email protected][_2_]

Global Variables in Excel VBA
 
If you put the variable in a module, it will be created when the
workbook opens and if preceded by the the word Public will be available
to all your forms and modules.

Hope that answers your question.

theSquirrel


wrote:
I have an excel program, heavily driven by menus and buttons with
various entry-points and exits.

I am however attempting to create a global variable that could be used
to store and access data, regardless of the entry/exit point used.

Use of the public command would create a global variable which would to
this. However, given the fact that a user may come in at any point
(depending on what button or menu they use), there is no obvious way to
create a global variable.

Is there a way to actually create a global variable - that could be
used regardless of what a user presses?



Randy Harmelink

Global Variables in Excel VBA
 
If worst comes to worst, you could always store any such data in the
workbook itself. Something akin to a "Settings" worksheet.

On Nov 5, 7:59 pm, wrote:

Is there a way to actually create a global variable - that could be
used regardless of what a user presses?



[email protected]

Global Variables in Excel VBA
 
Put this in a module:

Public Const GLOBAL_VAR as String

Then GLOBAL_VAR is available in any module.


Chip Pearson

Global Variables in Excel VBA
 
If worst comes to worst, you could always store any such data in the
workbook itself.


I wouldn't characterize that technique as something one would do if "worst
comes to worst". I've done that on more than a few occasions when I needed
to store a value and couldn't run the risk of globals getting wiped out by
the user monkeying around in the VBE. If you need to be certain that a
variable won't be wiped out of memory, you need to store it in a worksheet
cell, a defined name, or the registry. As long as you're not storing huge
number of variables in a sheet, to the point where performance would be
noticeably degraded, there's really nothing wrong at all with storing temp
data in a worksheet. Just make the sheet xlVeryHidden so the user can't
screw it up, and its a perfectly legitimate technique.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)


"Randy Harmelink" wrote in message
oups.com...
If worst comes to worst, you could always store any such data in the
workbook itself. Something akin to a "Settings" worksheet.

On Nov 5, 7:59 pm, wrote:

Is there a way to actually create a global variable - that could be
used regardless of what a user presses?





Stefi

Global Variables in Excel VBA
 
See Tom Ogilvy's response to my question "workbook-level public variables":

"declare it in a general module and initialize it in from any module

General Module:

Public MyVar as Long

in the ThisWorkbook Module

Private Sub Workbook_Open()
MyVar = 6
end Sub

--
Regards,
Tom Ogilvy"

Regards,
Stefi


ezt *rta:

I have an excel program, heavily driven by menus and buttons with
various entry-points and exits.

I am however attempting to create a global variable that could be used
to store and access data, regardless of the entry/exit point used.

Use of the public command would create a global variable which would to
this. However, given the fact that a user may come in at any point
(depending on what button or menu they use), there is no obvious way to
create a global variable.

Is there a way to actually create a global variable - that could be
used regardless of what a user presses?




All times are GMT +1. The time now is 01:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com