Solved

Storing variables in VBA

Posted on 2011-09-08
13
269 Views
Last Modified: 2012-06-21
I am new to VBA and am coding in Excel 2007.

I have learned about events and writing sub-routines that I can execute fromthe events. I need to have a few global variables though.

How do I do that?

Thanks.
0
Comment
Question by:newbieweb
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 250 total points
ID: 36505942
Define the variables in a general code module in the top declaration section as:

Public MyPublicVariable As Variant

Kevin
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 36505950
Any public (AKA global) variable defined this way is available to the entire project.

Kevin
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 100 total points
ID: 36505961
And you add a module by going to the 'Insert' menu item and selecting 'Module'.
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 100 total points
ID: 36505979
BTW I would 99.9% of the time not define the variable as Variant, but rather as String, Integer or whatever was appropriate.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36505981
Somewhat useful discussion here:

http://www.ozgrid.com/VBA/variable-scope-lifetime.htm

~bp
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 150 total points
ID: 36505994
Why do you need global variables?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:newbieweb
ID: 36506071
> Define the variables in a general code module in the top declaration section as:

In which sheet?

> Why do you need global variables?

I may need to limit the frequency with which I download data from the database. Maybe every 10 minutes, for a given table.

I am not sure but want to have the ability to do it if I need to...
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 36506085
A general code module is not the same as a sheet module.

Add a general code module by choosing "Module" from the "Insert" menu.

Kevin
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 36506108
A global variable (or constant) is an excellent choice for doing what you want. I frequently do the same.

A global constant looks like:

Public Const DatabaseCheckInterval As Long = 10 ' Minutes

Kevin
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 150 total points
ID: 36506120
I'm not sure global variables will limit that.

If you are populating something, let's say comboboxes, with the data it can be a one off.

You open the connection, get the data, populate the combobox, close the connection, that's it.
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 36506174
What the author is asking is reasonable if the workbook contains code that has a loop that pulls from the database every so many minutes and he wants the interval time to be easily editable.

Kevin
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 150 total points
ID: 36506449
Kevin

I know, but I'm still not sure what the author has/wants.

This question isn't totally clear and some of the others aren't either.
0
 

Author Closing Comment

by:newbieweb
ID: 36524131
Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now