Workbook_open event on multiple sheets

xL_challenged

New Member
Joined
Mar 30, 2011
Messages
4
I'm attempting a workbook_open event that runs on two sheets. When I push the "run" button in VBA the code works fine, but I can't get it to initiate when I open the workbook. And I'm not smart. Can you help? Here's what Ive got in "thisworkbook" module:

Private Sub workbook_open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
If [A1] = "" Then
.Unprotect Password:="secret"
.[A1] = DateValue(Date)
.Protect Password:="secret"
End If
End With
Next ws
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi
You have not qualified the cell in your If statement so that would probably cause problems i.e.

If .[A1] = "" Then

rather than this

If [A1] = "" Then

But that would cause problems regardless of whether you're running it manually or running automatically via the Workbook_Open. Anyway, let us know if this fixes your problem.

HTH
DK
 
Upvote 0
Hmm, that's odd - there is no matching End If for one of the If statements but it seems to be compiling....anyway, can you try this instead:

Code:
Private Sub workbook_open()
    Dim ws As Worksheet
        
    MsgBox "Workbook_Open"
    
    For Each ws In ThisWorkbook.Worksheets
        With ws
            If .[A1] = "" Then
                .Unprotect Password:="secret"
                .[A1] = DateValue(Date)
                .Protect Password:="secret"
            End If
        End With
    Next ws
End Sub
 
Upvote 0
That didn't run when I opened the workbook. when I ran it manually it gave me a run-time 1004 error.

Where do I need another End If statement?
 
Upvote 0
Don't worry - I had put in another If statement myself - your Ifs are fine!

What is strange is that your Workbook_Open procedure is not opening. I know that Excel 2000 had a problem with these sometimes - are you on that version? What I'd suggest next is to move your Workbook_Open procedure into the Auto_Open procedure in a standard module i.e. delete the Workbook_Open procedure and add in this to a standard module - does that work?

Code:
Private Sub Auto_Open()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        With ws
            If .[A1] = "" Then
                .Unprotect Password:="secret"
                .[A1] = DateValue(Date)
                .Protect Password:="secret"
            End If
        End With
    Next ws
End Sub
 
Upvote 0
I had been trying auto_open as well, but I didn't have that period (what'd you call it) before the cell number. Looks like that did it.

Thanks a lot. I had to take a break before I did something regrettable.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top