How to hide rows, but not blank rows used for formatting

H

hko78

Can you tell me how to solve the issue of not hiding the totally blank
rows that are part of the worksheet for formatting purposes, but do hide the
rows that have zeros in them based on formulas. I added the
hiderows/showrows to VBE, but it removes all the zero and blank rows.

Sub Hiderows()
Dim lr As Long, i As Long
With ActiveSheet
lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
If Cells(i, 3) = 0 And Cells(i, 4) = 0 Then
Cells(i, 1).EntireRow.Hidden = True
End If
Next
End With
End Sub

I would be most appreciative for a fast solution.

Thanks!

Heidi
 
J

JLGWhiz

Try this:

If Not IsBlank(Cells(i, 3)) And Cells(i, 3) = 0 _
And Not IsBlank(Cells(i, 4)) And Cells(i, 4) = 0 Then
 
H

hko78

Not sure if you mean to add this statement to the beginning of the hiderows
dialog.....if I do it gives me an error. Did you mean something else?
Sorry, not a very advanced VBE user.

Thanks so much for your help!!

Heidi
 
H

hko78

Scratch my first response.....If I add it into the existing hiderows dialog
to replace that one line it gives me the error "sub or function not defined"
and the 'IsBlank' is highlighted.

Does that help?

Thanks!

Heidi
 
H

hko78

No, that does not work either. It does not do anything when I try to run it.
Any other ideas?

Thanks for trying!!

Heidi
 
J

JLGWhiz

Let's try it this way:

If Cells(i, 3) <> "" And Cells(i, 3) = 0 _
And Cells(i, 4) <> "" And Cells(i, 4) = 0 Then

The point is that Excel sees blank, "" and 0 as being equal, but VBA can
tell the difference, so the statement has to be constructed so that it will
execute only when it finds a zero and not if it find a null string or blank.
There have been several postings on this but I did not copy them because I
try to avoid the need for them.
 
H

hko78

Still nothing. I've looked at several other posting, but thought it better
to get a fresh solution because I couldn't find one that mirrored my case.

Thanks!!
 
J

JLGWhiz

Last stab at this. It worked in my test once I used the right syntax. It
skipped the rows where the cell was blank but hid the rows where the cell
contained zero.

If IsEmpty(Cells(i, 3)) = False And Cells(i, 3) = 0 _
And IsEmpty(Cells(i, 4)) = False And Cells(i, 4) = 0 Then

If it still don't work in your code, then I am out of suggestions.
 
H

hko78

Is this exactly how it should look when I use your suggestion below? I just
want to make sure.

Thanks again for your assistance. I really appreciate it!

Sub Hiderows()
Dim lr As Long, i As Long
With ActiveSheet
lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
If IsEmpty(Cells(i, 3)) = False And Cells(i, 3) = 0 _
And IsEmpty(Cells(i, 4)) = False And Cells(i, 4) = 0 Then
Cells(i, 1).EntireRow.Hidden = True
End If
Next
End With
End Sub
 
H

hko78

Just wanted to let you know that this is what finally worked, changing the
range to suit my worksheet.

Thanks for all your help though!!

Heidi

Sub HideRows()
ActiveWindow.DisplayZeros = True
On Error Resume Next
Application.ScreenUpdating = False
With Range("e13:i600")
For i = 1 To .Rows.Count
If WorksheetFunction.CountA(.Rows(i)) > 0 Then
If WorksheetFunction.Sum(.Rows(i)) = 0 Then
.Rows(i).EntireRow.Hidden = True
Else
.Rows(i).EntireRow.Hidden = False
End If
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top