ThisWorkbook vs. ActiveWorkbook?
99% of the time this workbook is what you want to use. It refers to the
workbook that is executing the code. You only need to use activeworkbook if
you want to refer to some other work book besides the one executing the code.
That being said very often you see people using activeworkbook in place of
thisworkbook, because the book executing the code is the active workbook.
Technically it is not correct to do this but under most circumstances it will
work out just fine.
The long and the short of it is use thisworkbook unless you have a good
reason not to.
HTH
"JK" wrote:
Thank you for both answers. But, which is more apt to avoid conflicts,
ActiveWorkbook or ThisWorkbook?
Thank you.
Jim Kobzeff
"JK" wrote in message news:1zW8e.17644$Zn3.8439@trnddc02...
Please allow me to ask two questions:
(1) Which is the most fail-safe way to write in procedures: ThisWorkbook
or ActiveWorkbook?
Moreover, assuming it is ThisWorkbook, would I be required to write
Dim mySheet As Worksheet
Set mySheet= ThisWorkbook.Sheets(1)
Or can I simply write
Set mySheet=Sheets(1)
(2) I have a Clear Forms button on my toolbar that makes cell.Value="" on
a number of different sheets. Can I include Application.EnableEvents at
top and bottom as such,
Sub ClearForms()
Application.EnableEvents=False
With Sheets(1)
[code to clear cells]
End With
[additional With statements]
Application.EnableEvents=True
End Sub
or,
is it better to include it for each sheet action as such,
Sub ClearForms()
With Sheets(1)
Application.EnableEvents=False
[code to clear cells]
Application.EnableEvents=True
End With
[additional With statements]
End Sub
I'm just trying to make my application as bullet-proof against Excel
quirks as possible (if there is such a thing). Thank you in advance.
Jim Kobzeff
|