• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Code to hide rows with criteria

Karan Lodha

New Member
I am looking for a vba code which hides rows if the specific cells are blank on that row. As per the sheet attached I need to hide the row 11 if the C11 and D11 both are zero. if even one of the cell have values then it should not hide.
 

Attachments

  • Hide Column.xlsx
    8.7 KB · Views: 6
Code when cells are empty
Code:
Sub belle()
    Dim rng As Range, i As Long
    'Set the range in column C you want to loop through
    Set rng = Range("C3:C11")
    For Each cell In rng
        If cell.Value = "" And cell.Offset(, 1) = "" Then cell.EntireRow.Hidden = True
    Next
End Sub
Code when cells contain 0 (as in your example C11 and D11)
Code:
Sub belle()
    Dim rng As Range, i As Long
    'Set the range in column C you want to loop through
    Set rng = Range("C3:C11")
    For Each cell In rng
        If cell.Value = 0 And cell.Offset(, 1) = 0 Then cell.EntireRow.Hidden = True
    Next
End Sub
Combination of both
Code:
Sub belle()
  Dim rng As Range, i As Long
  'Set the range in column C you want to loop through
  Set rng = Range("C3:C11")
  For Each cell In rng
  If cell.Value = 0 And cell.Offset(, 1) = 0 Or cell.Value = "" And cell.Offset(, 1) = "" Then cell.EntireRow.Hidden = True
  Next
End Sub
 
Thanks for the reply.

But my requirement is if one of the cell in that particular row is non blank then it should not hide. However in this code it is taking into consideration only C Column.

It should only hide if there are no value in either cells of Column C and Column D for their respective component
 
Sorry to bother but the cell value derived is from formula. Will that work ?
 

Attachments

  • Hide Column.xlsx
    8.7 KB · Views: 2
I made an example, test it you will see that C and D have to be empty,as in your example (C10,C11 anf D10,D11)
 

Attachments

  • Hide Column.xlsb
    15.8 KB · Views: 4
Back
Top