Hiding rows based on cell content

D

Doug Howell

I am trying to do the following and don't know where to start:

Look at every third cell in a column range. (A14:A50)
If that cell's value is 0 then that row plus the following 2 rows are
hidden.


Thanks for any help.
 
N

Nigel

A starter for ten..... (Caution an empty cell is evaluated as 0) !

Sub test()
Dim c As Range
For Each c In Range("A10:A50")
If c.Value = 0 Then
Rows(c.Row & ":" & c.Row + 2).EntireRow.Hidden = True
End If
Next c
End Sub
 
D

Doug Howell

A starter for ten..... (Caution an empty cell is evaluated as 0) !

Sub test()
Dim c As Range
For Each c In Range("A10:A50")
  If c.Value = 0 Then
     Rows(c.Row & ":" & c.Row + 2).EntireRow.Hidden = True
  End If
Next c
End Sub

--

Regards,
Nigel
(e-mail address removed)








- Show quoted text -

Would the following work to ONLY look at every 3rd cell?

Sub test()
Dim c As Range
For Each c In Range("A10:A50")
If c.Value = 0 Then
Rows(c.Row & ":" & c.Row + 2).EntireRow.Hidden = True
End If
c=c+3
Next c
End Sub
 
N

Nigel

No because c is a range not a row reference. To do that use a control loop
something like...

Dim lRow as long
For lrow = 14 to 50 step 3
if Cells(lrow,1).value = 0 then
Rows(lrow & ":" & lrow + 2).EntireRow.Hidden = True
endif
Next

--

Regards,
Nigel
(e-mail address removed)



A starter for ten..... (Caution an empty cell is evaluated as 0) !

Sub test()
Dim c As Range
For Each c In Range("A10:A50")
If c.Value = 0 Then
Rows(c.Row & ":" & c.Row + 2).EntireRow.Hidden = True
End If
Next c
End Sub

--

Regards,
Nigel
(e-mail address removed)








- Show quoted text -

Would the following work to ONLY look at every 3rd cell?

Sub test()
Dim c As Range
For Each c In Range("A10:A50")
If c.Value = 0 Then
Rows(c.Row & ":" & c.Row + 2).EntireRow.Hidden = True
End If
c=c+3
Next c
End Sub
 
D

Doug Howell

Sheets("Bulk MO").Visible = True
Sheets("Bulk MO").Select
Sheets("Bulk MO").Unprotect
Dim lRow As Long
For lRow = 14 To 50 Step 3
If Cells(lRow, 1).Value = 0 Then
Rows(lRow & ":" & lRow + 2).EntireRow.Hidden = True
End If
Next
Sheets("Bulk MO").Protect
Sheets("Bulk MO").Visible = False


Gives an error:

Run-time error '1004':
Unable to set the Hidden property of the Range class
 
N

Nigel

Dim lRow As Long
With Sheets("Bulk MO")

.Unprotect

For lRow = 14 To 50 Step 3
If .Cells(lRow, 1).Value = 0 Then
.Rows(lRow & ":" & lRow + 2).EntireRow.Hidden = True
End If
Next

.Protect

End With
 
D

Doug Howell

Thanks for your patience with a programming neophyte Nigel..........
works perfectly now.......
 

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