Can I declare global arrays with VBA

MrTeeny

Board Regular
Joined
Jul 26, 2017
Messages
238
Can I declare a global array that's available to all modules within the workbook, I tried declaring a variant array as Public but it gave me an error
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Where did you try and declare the array and how did you try and populate it?
 
Upvote 0
Where did you try and declare the array and how did you try and populate it?

I declared it as a Public Variant on the sheet 1 object before running a Worksheet_Change event

Code:
Option ExplicitPrivate Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
Public priceArray() As Variant
 




Private Sub Worksheet_Change(ByVal Target As Range)........etc

I usually declare it within the Worksheet_Change event but want the data to be available to all modules

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim priceArray() As Variant....

works fine but obviously the array isn't avaliable all over
 
Upvote 0
It should be declared in a standard module not a class module like a sheet module.
 
Upvote 0
works fine but obviously the array isn't avaliable all over
Try declaring it in a general module (where you install macros) rather than in a sheet module (where you install event code) and do not declare it a second time in the event procedure (as your code above shows you doing)... then you should be able to ReDim it in the Change event (or any other event, macro, function, etc.) procedure to give it size and then fill it with values or, alternately, assign an exist or newly created array to it.
 
Upvote 0
It should be declared in a standard module not a class module like a sheet module.

Thanks , seems to all be working now .



I had a look at the link Fluff posted and could you tell me how I'd declare the variables as a project scope variable, I'm assuming the project scope means any variables are only available to that workbook and wouldn't get picked up or interfered with if I say had two versions of the workbook running?

At the moment I simply have

Code:
Option Explicit


Public priceArray() As Variant

But guessing I need to incorporate [FONT=&quot]Option Private Module[/FONT] within there somewhere
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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
Back
Top