Protect workbook question

  • Thread starter ryan.fitzpatrick3
  • Start date
R

ryan.fitzpatrick3

I have a workbook where I protected and have certain cells available
for data entry. I have a macro that hides and unhides rows.

Sub Hide()
Rows("19:22").Hidden = Not Rows("19:22").Hidden
Rows("66:69").Hidden = Not Rows("66:69").Hidden
Rows("114:117").Hidden = Not Rows("114:117").Hidden
Rows("158:161").Hidden = Not Rows("158:161").Hidden
Rows("254:257").Hidden = Not Rows("254:257").Hidden
Rows("299:302").Hidden = Not Rows("299:302").Hidden
Rows("341:344").Hidden = Not Rows("341:344").Hidden
Rows("208:210").Hidden = Not Rows("208:210").Hidden
Rows("372:1500").Hidden = Not Rows("372:1500").Hidden
ActiveWindow.SmallScroll Down:=-500
End Sub

Now when the workbook is protected this macro gives me an error, since
the sheet is protected. Is there code where if the sheet is protected
it'll disable this macro and enable it when the sheet isn't protected?
I have this macro in a combo box for a selection to choose from.
Thanks in advance.

ryan
 
B

Barb Reinhardt

Try this

Sub Hide()
Dim aWS As Worksheet
Set aWS = ActiveSheet
If Not aWS.ProtectContents Then
aWS.Rows("19:22").Hidden = Not aWS.Rows("19:22").Hidden
aWS.Rows("66:69").Hidden = Not aWS.Rows("66:69").Hidden
aWS.Rows("114:117").Hidden = Not aWS.Rows("114:117").Hidden
aWS.Rows("158:161").Hidden = Not aWS.Rows("158:161").Hidden
aWS.Rows("254:257").Hidden = Not aWS.Rows("254:257").Hidden
aWS.Rows("299:302").Hidden = Not aWS.Rows("299:302").Hidden
aWS.Rows("341:344").Hidden = Not aWS.Rows("341:344").Hidden
aWS.Rows("208:210").Hidden = Not aWS.Rows("208:210").Hidden
aWS.Rows("372:1500").Hidden = Not aWS.Rows("372:1500").Hidden
End If
ActiveWindow.SmallScroll Down:=-500

End Sub
 
R

ryan.fitzpatrick3

It didn't work for me.

Try this

Sub Hide()
Dim aWS As Worksheet
Set aWS = ActiveSheet
If Not aWS.ProtectContents Then
aWS.Rows("19:22").Hidden = Not aWS.Rows("19:22").Hidden
aWS.Rows("66:69").Hidden = Not aWS.Rows("66:69").Hidden
aWS.Rows("114:117").Hidden = Not aWS.Rows("114:117").Hidden
aWS.Rows("158:161").Hidden = Not aWS.Rows("158:161").Hidden
aWS.Rows("254:257").Hidden = Not aWS.Rows("254:257").Hidden
aWS.Rows("299:302").Hidden = Not aWS.Rows("299:302").Hidden
aWS.Rows("341:344").Hidden = Not aWS.Rows("341:344").Hidden
aWS.Rows("208:210").Hidden = Not aWS.Rows("208:210").Hidden
aWS.Rows("372:1500").Hidden = Not aWS.Rows("372:1500").Hidden
End If
ActiveWindow.SmallScroll Down:=-500

End Sub
 
B

Barb Reinhardt

I'm not sure why I didn't see this before, but you should have

Rows("19:22").EntireRow.Hidden
 

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