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,
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...
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...