Code not doing what I hoped for

  • Thread starter Patrick C. Simonds
  • Start date
P

Patrick C. Simonds

Can anyone tell me why the code below only works on the worksheet that's
active when it is run? My hope was that it would go to each worksheet
(excluding Holidays) and remove all shapes and clear the value in cell F1

Sub HolidayRemove()

Protection.UnProtectAllSheets

For n = 1 To Sheets.Count
If Sheets(n).Name <> "Holidays" Then
With Sheets(n)

ActiveSheet.Shapes.SelectAll
Selection.Delete
Range("K1").Value = ""

End With
End If

Next n

Protection.ProtectAllSheets
End Sub
 
M

Mike H

Hi,

Try this but be aware

ActiveSheet.Shapes.SelectAll
Selection.Delete

is a very blunt instrument and may delete more than you bargained for. try
it on a test workbook.


Sub HolidayRemove()
For n = 1 To Sheets.Count
If Sheets(n).Name <> "Holidays" Then
Sheets(n).Select
Sheets(n).Unprotect
With Sheets(n)
ActiveSheet.Shapes.SelectAll
Selection.Delete
Range("K1").Value = ""
Sheets(n).Protect
End With
End If
Next n
End Sub


Mike
 
G

Gary Keramidas

untested, but give it a try

Sub HolidayRemove()

Dim n As Long

For n = 1 To Sheets.Count
If Sheets(n).Name <> "Holidays" Then
With Sheets(n)
.Activate
.Unprotect
.Shapes.SelectAll
Selection.Delete
.Range("K1").Value = ""
.Protect
End With
End If

Next n


End Sub
 
P

Patrick C. Simonds

Thanks

Any way that can be made to return me to the worksheet and cell were the
code was triggered?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top