VBA to hide rows based on cell content

RosieRose

Board Regular
Joined
Jul 29, 2014
Messages
75
Hi there, I am trying to write a VBA to hide and entire row if any of the cells in four columns ( E,H,K and N) greater than -1 and small than 1. I have written the following code:
Code:
Sub Report()
Dim i As Integer
Dim Rows As Long
For i = 7 To 37
    If Cells(i, 5) > -1 < 1 And Cells(i, 8) > -1 < 1 And Cells(i, 11) > -1 < 1 And Cells(i, 14) > -1 < 1 Then
    ActiveCell.EntireRow.Select
    Selection.Hidden = True
    End If
Next i
End Sub
I am getting error message 1004: "Unable to set the hidden property of the range class. Any help would be greatly appreciated!
 
Thanks neonredsign. you are right. I should probably drink my coffee before I started working on this code. I would only to hide numbers with values in between -1 and 1. I played with the code you gace me, and I have it almost working perfectly. It doesn't hide the row if ALL the columns are greater than 1 or smaller than -1. I would like the code to NOT hide the row if ANY of the 4 columns show a value greater than 1 or smaller than -1. getting closer. I really do appreciate all your help!
Code:
Sub report()
Dim i As Integer
Dim j As Integer
    For i = 7 To 39
        For j = 5 To 14 Step 3
          
           If Cells(i, j) > -1 And Cells(i, j) < 1 Then
                Cells(i, j).EntireRow.Hidden = True
              
              ElseIf Cells(i, j) = "n/a" Then
                Cells(i, j).EntireRow.Hidden = True
           
           ElseIf Cells(i, j) <= -1 Or Cells(i, j) >= 1 Then
                Cells(i, j).EntireRow.Hidden = False
            End If
        Next j
    Next i

End Sub
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Thank you kindly to NeonRedSharpie and to Shoe1da. I did get the VBa to perform as needed. This is the code that worked:
Code:
Sub report2()
Dim i As Integer

    For i = 7 To 39
    
          
        If Cells(i, 5) > -1 And Cells(i, 5) < 1 And Cells(i, 8) > -1 And Cells(i, 8) < 1 And Cells(i, 11) > -1 And Cells(i, 11) < 1 And Cells(i, 14) > -1 And Cells(i, 14) < 1 Then
                Cells(i, 5).EntireRow.Hidden = True
     
         ElseIf Cells(1, 5) = "n/a" Then
         Cells(i, 5).EntireRow.Hidden = True
         
        ElseIf Cells(1, 8) = "n/a" Then
         Cells(i, 8).EntireRow.Hidden = True
         ElseIf Cells(1, 11) = "n/a" Then
         Cells(i, 11).EntireRow.Hidden = True
         ElseIf Cells(1, 14) = "n/a" Then
         Cells(i, 14).EntireRow.Hidden = True
      
          End If
 
    Next i

End Sub
thank you again for helping me work through this and for volunteering your time!!!
 
Upvote 0

Forum statistics

Threads
1,215,819
Messages
6,127,045
Members
449,356
Latest member
tstapleton67

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