Question about declaring variables

RunTime91

Active Member
Joined
Aug 30, 2012
Messages
290
Office Version
  1. 365
Experts -

What is the best way to declare DIM variables.

I'm running a script which bounces between a couple Excel books and several Sheets & Ranges.

With memory preservation in mind, Is the best way to declare these variables with as few DIM statements as possible, then set, clear (set to nothing) and reset?

E.g
Code:
This?
Setting all the variables at the beginning of the script

Dim Ws1 As Worksheet
Dim Ws2 As Worksheet
Dim Ws3 As Worksheet
Dim Ws4 As Worksheet
Dim Ws5 As Worksheet

Set Ws1 = Sheet1
Set Ws2 = Sheet2
Set Ws3 = Sheet3

Set Ws1 = Nothing
Set Ws2 = Nothing

Set Ws4 = Sheet4
Set Ws5 = Sheet5

Or This?
Setting the variables as you go

Dim Ws1 As Worksheet
Dim Ws2 As Worksheet
Dim Ws3 As Worksheet

Set Ws1 = Sheet1
Set Ws2 = Sheet2
Set Ws3 = Sheet3

Set Ws1 = Nothing
Set Ws2 = Nothing

Set Ws1 = Sheet4
Set Ws2 = Sheet5

I ask this because I tried setting, then clearing, then setting again as I went along and kept getting automation errors (all cleared up now) but it led me to ask this question

Thanks to all for your guidance (Google struggles to understand the context of my question)
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Are you sure you need a variable for each of the worksheets?
 
Upvote 0
Hey Norie - Good to hear from you...

I think so - I'm doing a bit of work in each of the sheets

What are your thoughts?
 
Upvote 0
Are you working on all 5 sheets at the same time?
 
Upvote 0
Ahh, there's the question that led to my post - no

I work on a couple, finish up then move on to a couple others and so on..,.
 
Upvote 0
If you are only working on, say, 2 sheets at one time all you really need are 2 variables for those sheets.

Once you've finished with the sheets you can then re-use the variables to refer to other sheets.

Could you post your code to let us see what it is you are doing?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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