hide xrange and protect cells

ho_gh_m

New Member
Joined
Oct 21, 2023
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
Hi
I have a formula :

VBA Code:
Dim xRg As Range
        For Each xRg In Range("A2:A21")
            If xRg.Value = "" Then
                If xRg.EntireRow.Hidden = False Then
                    xRg.EntireRow.Hidden = True
                End If
            Else
                If xRg.EntireRow.Hidden = True Then
                    xRg.EntireRow.Hidden = False
                End If
            End If
        Next xRg
    Application.ScreenUpdating = True

With this formula, the rows of a range where nothing is written are hidden ("")
But
When I protect the sheet, it gives error :

Run-time error '1004':
unable to set the Hidden property of the Range class

please help me
thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi
I have a formula :

VBA Code:
Dim xRg As Range
        For Each xRg In Range("A2:A21")
            If xRg.Value = "" Then
                If xRg.EntireRow.Hidden = False Then
                    xRg.EntireRow.Hidden = True
                End If
            Else
                If xRg.EntireRow.Hidden = True Then
                    xRg.EntireRow.Hidden = False
                End If
            End If
        Next xRg
    Application.ScreenUpdating = True

With this formula, the rows of a range where nothing is written are hidden ("")
But
When I protect the sheet, it gives error :

Run-time error '1004':
unable to set the Hidden property of the Range class

please help me
thanks
Rich (BB code):
Private Sub Worksheet_Activate()
Dim xRg As Range
'    Application.ScreenUpdating = False
        For Each xRg In Range("A2:A21")
            If xRg.Value = "" Then
                If xRg.EntireRow.Hidden = False Then
                    xRg.EntireRow.Hidden = True
                End If
            Else
                If xRg.EntireRow.Hidden = True Then
                    xRg.EntireRow.Hidden = False
                End If
            End If
        Next xRg
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Welcome to the MrExcel board!

A couple of options would be:
  1. When you protect the sheet, set the option to allow rows to be formatted.

    1697953077120.png


  2. Put a line at the start of your code to unprotect the worksheet and another line at the end of your code to re-protect it.

BTW, you can shorten that hide/unhide code a great deal. For example ..

VBA Code:
Private Sub Worksheet_Activate()
  Dim xRg As Range
  
  Application.ScreenUpdating = False
  For Each xRg In Range("A2:A21")
    xRg.EntireRow.Hidden = xRg.Value = ""
  Next xRg
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,968
Members
449,095
Latest member
Mr Hughes

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