Forum Discussion
Auto Hide/Unhide Rows based on Criteria and Range
Is possible to achieve this by modifying your existing code. You can use a loop to iterate through the specified range and hide or unhide rows based on your criteria.
Here is an example modification to include the second part of your requirement:
The VBA code is untested. Please back up your file in advance.
Sub HideRowsIfBlank()
Dim consultant As Range
Dim targetRange As Range
Dim cell As Range
' Set the initial consultant range
Set consultant = Range("A6")
' Loop through each consultant range
Do Until consultant.Value = ""
' Determine the target range based on the consultant range
Select Case consultant.Address
Case "$A$6": Set targetRange = Rows("5:8")
Case "$A$10": Set targetRange = Rows("9:12")
Case "$A$14": Set targetRange = Rows("13:25")
' Add more cases as needed for other consultant ranges
End Select
' Hide or unhide rows based on the consultant range value
If consultant.Value = 0 Then
targetRange.EntireRow.Hidden = True
Else
targetRange.EntireRow.Hidden = False
' Check for additional conditions (e.g., hide rows with 0 in a specific range)
For Each cell In Range("A14:A24")
If cell.Value = 0 Then
cell.EntireRow.Hidden = True
Else
cell.EntireRow.Hidden = False
End If
Next cell
End If
' Move to the next consultant range
Set consultant = consultant.Offset(4, 0)
Loop
End SubThis modified code uses a loop to go through each consultant range, and within each range, it hides or unhides rows based on the consultant range value. Additionally, it checks a specific range (A14:A24 in this example) and hides rows with 0 in column A if the consultant range value is not 0. You can customize the code by adding more cases for other consultant ranges as needed.
The text, steps and vba code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- TracyNakamuraFeb 05, 2024Copper Contributor
NikolinoDE This is awesome! I feel like it's almost there. Is it possible to set different target ranges for the additional conditions? Or can I tell it to use the targetRange in the case? I tried editing it to "For Each Cell In targetRange" and "consultant.Range" and that did not work.
' Check for additional conditions (e.g., hide rows with 0 in a specific range) For Each cell In Range("A14:A24") If cell.Value = 0 Then cell.EntireRow.Hidden = True Else cell.EntireRow.Hidden = False End IfQuick question about this one:
' Move to the next consultant range Set consultant = consultant.Offset(4, 0)What is the #4 mean in this? Does it only go through 4 lines?
Thank you!