Inconsistent macro behavior

D

davegb

Can anyone tell me why this macro turns on the gridline display on some
worksheets, but not others, in the workbook? It's supposed to show all
sheets, unprotect all sheets, and turn on gridlines on all sheets.

Sub AllSheetsUnhideUnProtectWGrid()
'for all sheets in currently active workbook, assigned to button
'Password
Dim WkSht As Worksheet
Dim PWORD As String
PWORD = "dave"
Application.ScreenUpdating = False

For Each WkSht In ActiveWorkbook.Worksheets
If Not WkSht.Visible Then
WkSht.Visible = True
End If

WkSht.Unprotect Password:=PWORD
ActiveWindow.DisplayGridlines = True
Next WkSht

Application.ScreenUpdating = True

End Sub

As always, thanks in advance!
 
T

Tom Ogilvy

Sub AllSheetsUnhideUnProtectWGrid()
'for all sheets in currently active workbook, assigned to button
'Password
Dim WkSht As Worksheet
Dim PWORD As String
PWORD = "dave"
Application.ScreenUpdating = False

For Each WkSht In ActiveWorkbook.Worksheets
If Not WkSht.Visible Then
WkSht.Visible = True
End If
Wksht.Activate
WkSht.Unprotect Password:=PWORD
ActiveWindow.DisplayGridlines = True
Next WkSht

Application.ScreenUpdating = True

End Sub
 
D

davegb

Thanks Tom!

Tom said:
Sub AllSheetsUnhideUnProtectWGrid()
'for all sheets in currently active workbook, assigned to button
'Password
Dim WkSht As Worksheet
Dim PWORD As String
PWORD = "dave"
Application.ScreenUpdating = False

For Each WkSht In ActiveWorkbook.Worksheets
If Not WkSht.Visible Then
WkSht.Visible = True
End If
Wksht.Activate
WkSht.Unprotect Password:=PWORD
ActiveWindow.DisplayGridlines = True
Next WkSht

Application.ScreenUpdating = True

End Sub
 

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