meghant...@hotmail.com wrote...
>I need to run a Macro named UpdateData upon open of the file.
>This code is not working. I placed the below code in ThisWorkbook.
>I am able to manually run UpdateData, but it does not run
>automatically when I open the file.
>Security is set to Medium and I enable Macro's when I open the file.
>What am I doing wrong?
>
>Private Sub Workbook_Open()
> Call UpdateData
>End Sub
If for some reason Application.EnableEvents had been set to FALSE, no
event handlers would run, not even Open events. However, with the file
open, you could define the name Auto_Open referring to
='YourFilenameHere.xls'!ThisWorkbook.Workbook_Open
and it'll run on opening if you enable macros even if EnableEvents
were set to FALSE. However, it'd run twice if EnableEvents were set to
TRUE. You'd need to add a state variable to prevent that.
Private Sub Workbook_Open()
Static st As Boolean
If Not st Then
Call UpdateData
st = True
End If
End Sub
Even so, if UpdateData throws a runtime error, and you end it, the
static variable st will reset to FALSE, so this would try to run
UpdateData a second time. You could use a defined name or a worksheet
cell as the state variable, but that'd mark the file as modified
(which may not be a big deal).
|