How do I add to a autorun macro when workbook open

ghrek

Active Member
Joined
Jul 29, 2005
Messages
426
Hi

I have the following macro that deletes data in non protected cells of which I want to keep but I also want it to select page called "INSTRUCTIONS" when its run.

Any Ideas?
VBA Code:
Private Sub Workbook_Open()
 Dim ws As Worksheet
Dim cl As Range

For Each ws In ActiveWorkbook.Sheets
For Each cl In Sheets(ws.Name).UsedRange.Cells
If cl.Locked = False Then
cl.ClearContents
End If
Next cl
Next ws
'
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
VBA Code:
Option Explicit

Private Sub Workbook_Open()
Dim ws As Worksheet
Dim cl As Range

    For Each ws In ActiveWorkbook.Sheets                '<--- This line of code selects all sheets
        For Each cl In Sheets(ws.Name).UsedRange.Cells
            If cl.Locked = False Then
                cl.ClearContents
            End If
        Next cl
    Next ws
'
End Sub

As shown in the code above, the annotated line selects all sheets. Why is your macro not
selecting sheet INSTRUCTIONS as well ? Have you protected it or something else ?
 
Upvote 0
the sheets are protected andthe sheet called INSTRUCTIONS was added later but will also be protected

Sorry just read what I posted again and what I meant was I need it to go to sheet called INSTRUCTIONS after macro has run.

SORRY
 
Upvote 0
VBA Code:
 If cl.Locked = False Then
                cl.ClearContents
            End If
        Next cl
    Next ws
    
Sheets("INSTRUCTIONS").Select
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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