Help with VBA code to hide rows with zero values

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
148
Office Version
  1. 365
Platform
  1. Windows
Hello. Below is some code I wrote which hides/unhides certain rows when a selection is made from a drop down list. It runs great with no issues. My question is this. In the first grouping I am asking rows 16-43 to be unhidden along with rows 15 and 101 which are always to remain unhidden. Now I want to add another rule which states that in those unhidden rows (16-43), hide the rows that are showing a zero value in a particular column (in this case it is column BE). How do I add a second condition which hides any row in that range (rows 16-43) that have a zero value in column BE?

VBA Code:
Sub ListSelection()

    If Range("K2").Value = "Precision Diagnostics" Then
        Rows("15").EntireRow.Hidden = False
        Rows("16:43").EntireRow.Hidden = False
        Rows("44:100").EntireRow.Hidden = True
        Rows("101").EntireRow.Hidden = False
   
    ElseIf Range("K2").Value = "IGT" Then
        Rows("15").EntireRow.Hidden = False
        Rows("45:47").EntireRow.Hidden = False
        Rows("16:44").EntireRow.Hidden = True
        Rows("48:100").EntireRow.Hidden = True
        Rows("101").EntireRow.Hidden = False

    ElseIf Range("K2").Value = "Connected Care" Then
        Rows("15").EntireRow.Hidden = False
        Rows("49:89").EntireRow.Hidden = False
        Rows("16:48").EntireRow.Hidden = True
        Rows("90:100").EntireRow.Hidden = True
        Rows("101").EntireRow.Hidden = False
       
    ElseIf Range("K2").Value = "Health Systems Equipment" Then
        Rows("15").EntireRow.Hidden = False
        Rows("91:93").EntireRow.Hidden = False
        Rows("16:90").EntireRow.Hidden = True
        Rows("94:100").EntireRow.Hidden = True
        Rows("101").EntireRow.Hidden = False
       
    ElseIf Range("K2").Value = "Health Systems" Then
        Rows("15").EntireRow.Hidden = False
        Rows("95:98").EntireRow.Hidden = False
        Rows("16:94").EntireRow.Hidden = True
        Rows("99:100").EntireRow.Hidden = True
        Rows("101").EntireRow.Hidden = False
       
    ElseIf Range("K2").Value = "Health Tech" Then
        Rows("15:101").EntireRow.Hidden = False

End If


End Sub
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try replacing...

VBA Code:
Rows("16:43").EntireRow.Hidden = False

with

VBA Code:
Dim currentCell As Range
For Each currentCell In Range("BE16:BE43")
    If currentCell.Value = 0 Then
        currentCell.EntireRow.Hidden = True
    Else
        currentCell.EntireColumn.Hidden = False
    End If
Next currentCell

Or, simply...

VBA Code:
Dim currentCell As Range
For Each currentCell In Range("BE16:BE43")
    currentCell.EntireRow.Hidden = (currentCell.Value = 0)
Next currentCell

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

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