简体   繁体   中英

VBA Hide/Unhide Rows based on specific answers

I am trying to hide/unhide rows based on specific cell values. So far my code works and is below: However, I am also trying to show rows between the "yes" "no" rows. for instance, row 11-15 begins as shown. Row 15 has "yes" or "no" answers. After choosing "yes", I need to show 16-20. but as of now, I can only show 20 (column 8 is the selection for yes/no and column 11 is the offset and column 12 currently contains the number to skip to... so row 15 column 12 contains "20"... but I need it to be 16-20). How do I solve this? Thank you

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then

For Each cel In Target
       Call Worksheet_Change(cel)
       Next cel
End If
If Target.Column = 8 Then

   If LCase(Target.Value) = LCase(Target.Offset(, 3)) Then
   Cells(Target.Offset(, 4), 1).EntireRow.Hidden = False
Else

   Cells(Target.Offset(, 4), 1).EntireRow.Hidden = True

End If:  End If

End Sub

The easiest way to do this is to using a loop. What you want to do is hide each row in a loop, for example this loop will hide Rows 1-3

For i=1 to 3
    Rows(i).EntireRow.Hidden = True
Next

If I understnad your setup correctly column 8 contains "yes/no". Column 11 contains a row offset to start (un)hiding rows. Column 12 tells where to stop (un)hiding rows.

I will use the following notation to indicate a cell address (row, column)

Back to your example if (15,8) says "yes" then you unhide rows 16,17,18,19, 20 . This means (15,11) would contain 1 since the offset to get to row 16 is the current_row + 1, where current row is 15 cell (15,12) contains 20 since it is the last row to skip to. Simply use the value from cell (15,11) as the start of your loop and the value in cell (15,12) as the stop value

 Private Sub Worksheet_Change(ByVal Target As Range) 'defines some constants Const iYES_NO_COL = 8 Const iOFFSET_COL = 11 Const iSKIP_TO_COL = 12 If Target.Count > 1 Then For Each cel In Target Call Worksheet_Change(cel) Next cel End If ElseIf Target.Count = 1 Then 'im not sure what this does so i left it If Target.Column = 8 Then If LCase(Target.Value) = LCase(Target.Offset(, 3)) Then Cells(Target.Offset(, 4), 1).EntireRow.Hidden = False Else Cells(Target.Offset(, 4), 1).EntireRow.Hidden = True End If If (Target.Column = iYES_NO_COL) Then ' takes the current row + the value in the offset cell my_start = Target.Row + Cells(Target.Row, iOFFSET_COL).Value ' takes the value from the SKIP_TO_COL my_stop = Cells(Target.Row, iSKIP_TO_COL).Value 'target should be only one cell at this point, see if it 'contains the word no If (StrComp(Trim(LCase(Target.Value)), "no") = 0) Then 'hides all the rows between the start and stop value For i = my_start To mystop Rows(i).EntireRow.Hidden = True Next ElseIf (StrComp(Trim(LCase(Target.Value)), "yes") = 0) Then 'unhides all the rows between the start and stop value For i = my_start To mystop Rows(i).EntireRow.Hidden = False Next End If End If End Sub 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM