Hiding certain rows depending on cell value

Phil360

New Member
Joined
Jul 23, 2014
Messages
2
Hi all,

I'm fairly new to VBA programming. Im actually creating a BOM (Bill of Material) in Excel, and I'm simply trying to hide the rows where the item quantity is 0 when clicking a command button. It looks like this:

Private Sub CommandButton1_Click()


If Worksheets("BOM").Range("D3").Value = 0 Then
Worksheets("BOM").Rows("3:3").Hidden = True
End If


If Worksheets("BOM").Range("D4").Value = 0 Then
Worksheets("BOM").Rows("4:4").Hidden = True
End If


If Worksheets("BOM").Range("D5").Value = 0 Then
Worksheets("BOM").Rows("5:5").Hidden = True
End If


If Worksheets("BOM").Range("D6").Value = 0 Then
Worksheets("BOM").Rows("6:6").Hidden = True
End If

.....

It works fine, but there is over 300 rows. Is there a way to do a similar command for all the rows?

Thanks,

Phil
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to the Board!

This can be done pretty easily using Filters.
Code:
[COLOR=#333333]Private Sub CommandButton1_Click()
[/COLOR]

    Dim myRange As Range


'   Select all rows in column D with data
    Set myRange = Range("D1:D" & Cells(Rows.Count, "D").End(xlUp).Row)
    
'   Filter
    myRange.AutoFilter Field:=1, Criteria1:="<>0", Operator:=xlAnd
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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