ThisWorkbook vs. ActiveWorkbook?
"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?
It depends. If you want to refer to the workbook that the executing coede is
in, use ThisWorkbook. If you want to refer to the currently activated
workbook, which could be another workbook if say you open a workbook, then
use ActiveWorkbook.
Moreover, assuming it is ThisWorkbook, would I be required to write
Dim mySheet As Worksheet
Set mySheet= ThisWorkbook.Sheets(1)
Yes
Or can I simply write
Set mySheet=Sheets(1)
.... as this will default to the Activeworkbook.
(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
Best to add error handling in case something goes wrong, don't leave
Application.EnableEvents off
Sub ClearForms()
On Error Goto cf_exit:
Application.EnableEvents=False
With Sheets(1)
[code to clear cells]
End With
[additional With statements]
cf_exit:
Application.EnableEvents=True
End Sub
|