VBA Code (Hide Rows)

Range

Board Regular
Joined
Nov 13, 2010
Messages
140
Hi,

I need some VBA code to hide some rows depending on a ">0" criteria in column P.

Thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try:
Code:
Public Sub HideRows()
Dim r As Range
Dim lLastRow As Long
lLastRow = Range("P" & Rows.Count).End(xlUp).Row
For i = 1 To lLastRow
    If Range("P" & i).Value > 0 Then
        If r Is Nothing Then
        Set r = Range("P" & i)
        Else
        Set r = Union(r, Range("P" & i))
        End If
    End If
Next i
If Not r Is Nothing Then r.EntireRow.Hidden = True
End Sub
 
Upvote 0
Ahh brilliant, thanks very much. But one more thing... IF I wanted to do it when cell = 0 how would I ensure that the cells without a sum in there are not hidden?

Would it be better to go with a "true" criteria instead?
 
Upvote 0
try
Code:
Public Sub HideRows()
Dim r As Long, lr As Long
lr = Cells(Rows.Count, "P").End(xlUp).Row
For i = lr To 1 Step -1
    If Range("P" & i).Value >= 0 Then
    Rows(i).EntireRow.Hidden = True
    End If
Next i
End Sub
 
Upvote 0
you can use Filter by code
if your data from A:p
try this code
Code:
Sub Test()
Dim lR As Long
lR = Range("P" & Rows.Count).End(xlUp).Row
With Range("A1:P" & lR)
    .AutoFilter 16, ">0"
End With
End Sub
 
Upvote 0
Ahh brilliant, thanks very much. But one more thing... IF I wanted to do it when cell = 0 how would I ensure that the cells without a sum in there are not hidden?

Would it be better to go with a "true" criteria instead?
Sorry, I didn't understand. Does it mean:
"All rows with column P values equal to zero shall be hidden except where a sum formula is used and it is showing zero."?
 
Upvote 0
Sorry Guys,

I only want this code to hide rows 13:31....in column P.... what would I change? At the moment it hides alot of info that i still need

Sorry that I keep changing my mind!!
 
Upvote 0
Range, try this just to hide rows from 13 to 31
Code:
Sub Test()
Rows("13:31").Hidden = True
End Sub
 
Upvote 0
Sorry, I still need the criteria sum=0 to be the reason why the row is hidden!! :):):)


same with Michael M Code
Code:
Public Sub HideRows()
Dim i As Integer
For i = 31 To 13 Step -1
    If Range("P" & i).Value >= 0 Then
    Rows(i).EntireRow.Hidden = True
    End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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