To Global or not to Global

dugdugdug

Active Member
Joined
May 11, 2012
Messages
342
I use some of my variables in more than one module.

In Module1, I have declared:

Code:
Sub Proc1

Dim wsg As Worksheet
Set wsg = Worksheets("Graph")

End Sub

In Module2, I do the same when I want to use it.

Question is, would it be better to declare wsg as a Global variable or not?

Does it save time / memory or is it negliable?

Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
As you're just referencing a spreadsheet object in memory, in this case a graph, then there is not a lot of point in maintaining a global pointer to it.

You would only consider making a variable global if it was a custom-object that needed maintaining between procedures, or, any sort of value-type that again was not already available from the spreadsheets.
 
Upvote 0
As you're just referencing a spreadsheet object in memory, in this case a graph, then there is not a lot of point in maintaining a global pointer to it.

You would only consider making a variable global if it was a custom-object that needed maintaining between procedures, or, any sort of value-type that again was not already available from the spreadsheets.

But I have other examples where I have declared in a module:

Code:
Global myarray() As Variant

because I use myarray() in several modules.

So should that also just be declared in the declaration section of a module simply as Dim myarray() As Variant instead of globalling it?
 
Upvote 0
If you need to use the contents of myarray() elsewhere then yes. If you just want to re-use the structure, then no. Just declare it again.
 
Upvote 0

Forum statistics

Threads
1,203,601
Messages
6,056,211
Members
444,850
Latest member
dancasta7

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