tags:

views:

52

answers:

2

Hi Everyone,

I want to run a VBA macro AFTER the workbook has finished opening. I tried to use workbook_open but this runs before the workbook has finished opening. This doesn't work for me as I need to loop through each sheet like so...

Private Sub Workbook_Open()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
'do stuff on each sheet
Next ws

End Sub

Does anyone know if there is an event which runs once the workbook has finished opening? Or have any other suggestions on how I can achieve this?

A: 

Try using ThisWorkbook rather than ActiveWorkbook:

Private Sub Workbook_Open()
    Dim osht As Worksheet
    For Each osht In ThisWorkbook.Worksheets
        Debug.Print osht.Name
    Next osht
End Sub
Charles Williams
+2  A: 

Put your code in the Workbook_Activate event. It happens after the Open event.

Private Sub Workbook_Activate()
    ' Code goes here
End Sub
Michael
Thanks for your reply, for some reason this doesn't do anything at all. Once the workbook has loaded it doesn't initiate this? Could it be something to do with the security settings?
samcooper11
Try ThisWorkbook_Activate rather than Workbook_Activate
Jazza
It could be a security issue. I just tested code in that event and it seemed to run fine on my machine.
Michael