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
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)
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: