Just a tweak in a HideRows VBA Code

mikeb7500

Board Regular
Joined
Jul 30, 2014
Messages
98
Happy New Year! I feel I'm close but getting an Run-Time '1004": error message. "Unable to Set the Hidden property of the Range class."

I want to SHOW rows 25:33 if the G23 says "Yes", if it says anything else, I want rows 25:33 to be HIDDEN.

I feel I'm close but need a bit of tweaking. Please help...THANKS!

VBA Code:
Private Sub worksheet_change(ByVal target As Range)
Dim KeyCells As Range
Set KeyCells = Range("G23")

If Not Application.Intersect(KeyCells, Range(target.Address)) Is Nothing Then
If KeyCells = "Yes" Then
Rows("25:33").Hidden = False
Else
Rows("25:33").Hidden = True
End If
End If

End Sub
 
Last edited by a moderator:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
The code works when it's unprotected, but doesn't when I protect it.

Try adding some additional code that unprotects the worksheet

untested but something like this maybe

Rich (BB code):
Private Sub worksheet_change(ByVal Target As Range)
    Dim KeyCells    As Range
    
    Const MyPassword As String = "YourPasswordHere"
    
    Set KeyCells = Range("G23")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        Me.Unprotect MyPassword
        
        If KeyCells = "Yes" Then
            Rows("25:33").Hidden = False
        Else
            Rows("25:33").Hidden = True
        End If
    End If
    
    Me.Protect MyPassword
End Sub

Dave
 
Upvote 0
@mikeb7500
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in post 1 for you this time. 😊


I also suggest that you update your forum profile (click your user name at the top right of the forum, then ‘Account details’) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)


There is no need for Range(Target.Address) since target already is a range. Instead, you can just use this.
Rich (BB code):
If Not Application.Intersect(KeyCells, target) Is Nothing Then


Finally, you could replace all this code
VBA Code:
If KeyCells = "Yes" Then
Rows("25:33").Hidden = False
Else
Rows("25:33").Hidden = True
End If
End If
with this
VBA Code:
Rows("25:33").Hidden = KeyCells <> "Yes"
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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