Hiding/showing rows between two values

Naga81

New Member
Joined
Jul 11, 2017
Messages
3
i am trying to hide rows in excel between a row with value "P1" in column B and a row with value "P2" also in column B. i have tried, to my own shame i used Chatgpt for a part but i am stuck now. a part of the code succeeded in hiding the rows, but i was not able to let them reapear when i pushed the button. some of the things i tried are commented out,

VBA Code:
Private Sub CommandButton1_Click()

    Dim ws As Worksheet
    Dim startRow As Long
    Dim endRow As Long
    Dim cell As Range
    Dim Rng As Range
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Calculatie")
    
    ' Find the start and end rows based on values in column B
    For Each cell In ws.Range("B:B")
        If cell.Value = "P1" And startRow = 0 Then
            startRow = cell.Row
        ElseIf cell.Value = "P2" And startRow <> 0 Then
            endRow = cell.Row
            Exit For
        End If
    Next cell
    
    'Set Rng = Selection
    
    'Set Rng = ws.Rows(startRow + 1 & ":" & endRow - 1)
    'Set Rng = ws.Range("startRow", "endRow")
    
    
    ' Hide rows between startRow and endRow
    If startRow > 0 And endRow > 0 Then
    'If Rng.Hidden = True Then
        'Selection.EntireRow.Hidden = True
        ws.Rows(startRow + 1 & ":" & endRow - 1).EntireRow.Hidden = True
    Else
        'Selection.EntireRow.Hidden = False
        'ws.Rows(startRow + 1 & ":" & endRow - 1).EntireRow.Hidden = False
        MsgBox "Start or end value not found in column B"
    End If
End Sub

what do i need to change to get this working?

i tried to make a range and change the hidden value, gave errors... so i put i back to the original code that worked...
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Your code seems to work just fine for me. I would just make one change.
Change this line:
VBA Code:
    If startRow > 0 And endRow > 0 Then
to this:
VBA Code:
    If startRow > 0 And endRow > 0 And (endRow - startRow > 1) Then

This handle the case where there are no rows between P1 and P2.

If you are still not getting results, double-check your values in column B. The cells with "P1" and "P2", is that all there is in them?
Are they EXACTLY 2 characters long (no extra spaces or special characters)?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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