global vars (esp. array)

codesmith

Active Member
Joined
Apr 23, 2008
Messages
257
Hey all ... wanting to know how I can define a global array such that any of my sub routines or functions can view/edit it?

Thanks in advance.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
For variables in the same project (Excel file with all the modules, worksheets, userforms), you just basically declare the variable as a public variable in a standard module, which means at the top before any subs or functions and with the keyword Public:

Code:
Public gstrMyName as String

Sub UseIt()
Msgbox gstrMyName
End Sub

I use prefix conventions for public and private variables (variables with scope outside the current procedure):

g for global variable - project level scope
p for private variables - module level scope

Followed of course by the datatype:
i.e.
pintNumber = a private integer,
gblnTrueOrFalse = a public boolean,
plngCount = a private long,
etc. etc.


I guess for reasons I don't understand I usually don't use prefixes for arrays, but if I did I think I'd use arr, I.e. arrMyArray or garrMyArray / parrMyArray - I suppose its in the nature of array variables that they are easily recognized by the manner of their use...

AB
HTH
 
Last edited:
Upvote 0

Forum statistics

Threads
1,190,613
Messages
5,981,939
Members
439,744
Latest member
Lazerbeakk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top