Keeping the dimensioning at the top is just preference. Those lines are only compiled and not ran (traditionally in the sense). The only difference would be is if your variables were declared
outside of your sub routine. That would make them (by default) public, unless otherwise specified. Here are some examples ...
<font face=Tahoma New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>
<SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Compare</SPAN> <SPAN style="color:#00007F">Text</SPAN>
Global <SPAN style="color:#00007F">Const</SPAN> NL <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = vbNewLine
Global <SPAN style="color:#00007F">Const</SPAN> APPNAME <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "My Application"
<SPAN style="color:#00007F">Public</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Public</SPAN> rngTest <SPAN style="color:#00007F">As</SPAN> Range
<SPAN style="color:#00007F">Public</SPAN> strFilePath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
<SPAN style="color:#00007F">Private</SPAN> lngCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Private</SPAN> blnCreated <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
<SPAN style="color:#00007F">Sub</SPAN> Blahblahblah()
<SPAN style="color:#007F00">'Can be used in any module..</SPAN>
<SPAN style="color:#00007F">Set</SPAN> ws = Worksheets("Sheet1")
<SPAN style="color:#007F00">'Can be used in any module..</SPAN>
<SPAN style="color:#00007F">Set</SPAN> rngTest = ws.Range("A1:B2")
<SPAN style="color:#007F00">'Can be used in any module..</SPAN>
strFilePath = rngTest(1, 1).Value
<SPAN style="color:#007F00">'Can be used in ONLY this module..</SPAN>
lngCount = rngTest.Cells.Count
<SPAN style="color:#007F00">'Can be used in ONLY this module..</SPAN>
blnCreated = (lngCount = rngTest.Cells.Count)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
The
reason your code didn't work for anything except the active sheet was because of lines like this ..
Code:
Range("Q694:DJ694") = Range("Q1:DJ1").Value
Set rng1 = Range("Q693:DJ693")
All of those ranges are unqualified. Meaning you define a worksheet (ws) but don't specify those ranges to be on that sheet. With nothing there, Excel will always assume it's the activesheet. If no activesheet is there, your code will fail with a range class failure. If you wanted to fully qualify your code, just add your worksheet variable to the ranges...
Code:
ws.Range("Q694:DJ694") = ws.Range("Q1:DJ1").Value
Set rng1 = ws.Range("Q693:DJ693")
That makes all the difference in the world. Do the same for workbooks if you are working with more than one workbook via code. Or use With/End With statements for them (which, if used correctly, may even speed up your code a bit).