Macro to hide rows

kellerh3

Board Regular
Joined
Jul 13, 2007
Messages
117
I'm trying to figure out a way to hide a dynamic range of rows when a particular cell becomes active either via a mouse click or cell navigation using the arrow keys. For instance, in cell A1, i have the word "Hide" while the range "Data" is a series of rows that increases or decreases base on how much data there is. What i'd like to have happen is when either the mouse clicks on A1 or the cursor is moved to a1, the "Data" range is hidden and the word "Show" appears in A1. When A1 is clicked again, the "Data" range appears again.

Can anyone help with this as i am just learning macro code...
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Put this code in the module for the Worksheet:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .Address <> "$A$1" Then Exit Sub
        If .Value = "Hide" Then
            .Value = "Show"
        Else
            .Value = "Hide"
        End If
    End With
    With Range("Data").EntireRow
        .Hidden = Not .Hidden
    End With
End Sub
 
Upvote 0
I must be doing something wrong as i tried putting it into both a module and class module and nothing happens. Doesnt matter if i click on A1 or navigate to it, "Data" does not hide.
 
Upvote 0
While the rows in Data are visible, enter Hide in A1, then select another cell. Right click the sheet tab and choose View Code. Paste the code into the window on the right. Press Alt+F11 to return to your worksheet. Try it out by selecting A1.

If it doesn't work make sure macros are enabled.
 
Upvote 0
If i would like to hide different rows within the same sheet using the method above, do i change the "Worksheet_SelectionChange(ByVal Target As Range)" or do i modify something else?
 
Upvote 0
I've tried copying the existing like below but this doesnt work either. Can you provide any direction on what to change?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Target
If .Address <> "$A$1" Then Exit Sub
If .Value = "Hide" Then
.Value = "Show"
Else
.Value = "Hide"
End If
End With
With Range("Data").EntireRow
.Hidden = Not .Hidden
End With
Range("B1").Select
With Target
If .Address <> "$D$10" Then Exit Sub
If .Value = "Hide" Then
.Value = "Show"
Else
.Value = "Hide"
End If
End With
With Range("Objectives").EntireRow
.Hidden = Not .Hidden
End With
Range("C10").Select
End Sub
 
Upvote 0
The code exits if Target is not A1, so it never gets to test for D10. I changed it to test for equality rather tha inequality:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .Address = "$A$1" Then
            If .Value = "Hide" Then
                .Value = "Show"
            Else
                .Value = "Hide"
            End If
            With Range("Data").EntireRow
                .Hidden = Not .Hidden
            End With
            Range("B1").Select
        ElseIf .Address = "$D$10" Then
            If .Value = "Hide" Then
                .Value = "Show"
            Else
                .Value = "Hide"
            End If
            With Range("Objectives").EntireRow
                .Hidden = Not .Hidden
            End With
            Range("C10").Select
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,559
Messages
6,114,302
Members
448,564
Latest member
ED38

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