Macro to hide entire row if value from 2 columns are blank

BenR

New Member
Joined
Jan 4, 2006
Messages
22
Hello,
I require a macro to hide entire rows based on the criteria of having blank values in BOTH columns C & D.

Thanks in advance

BENR
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello BENR

This is how you would do it manually:

Highlight columns C:D
Go to the data tab > filter
In the filter drop down for each filter, scroll to the end of the list and uncheck 'Blanks'

You should be able to follow these steps with macro recorder to get the VBA code.
 
Upvote 0
try
Code:
Sub MM1()
Dim r As Long
For r = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
    If Range("C" & r).Value = "" And Range("D" & r).Value = "" Then Rows(r).Hidden = True
Next r
End Sub
 
Upvote 0
One hit:
Code:
Public Sub HideBlankRows()
    Dim rngToCheck As Excel.Range


    With Excel.ActiveSheet
        Set rngToCheck = Excel.Intersect(.Columns("C:D"), .UsedRange)
    End With


    With rngToCheck
        Call .AutoFilter(Field:=1, Criteria1:="<>", Operator:=xlAnd, VisibleDropDown:=False)
        Call .AutoFilter(Field:=2, Criteria1:="<>", Operator:=xlAnd, VisibleDropDown:=False)
    End With
End Sub
 
Upvote 0
Can we also try similar but EITHER blank values in columns C or D hide row
Sorry - I shauld have asked for this option aswell first time around as well..
I require EITHER blank values in columns C or D to hide row.
Thanks inadvance.
 
Upvote 0
Just change the AND to OR
Code:
Sub MM1()
Dim r As Long
For r = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
    If Range("C" & r).Value = "" OR Range("D" & r).Value = "" Then Rows(r).Hidden = True
Next r
End Sub
 
Upvote 0
you could modify Michael M's macro :
If Range("C" & r).Value = "" And Range("D" & r).Value = "" Then Rows(r).Hidden = True
to
If Range("C" & r).Value = "" OR Range("D" & r).Value = "" Then Rows(r).Hidden = True

Or you could have 3 of Jon con der Heyden's macro's,

Hideblankrowsc()
change: Call .AutoFilter(Field:=2, Criteria1:="<>", Operator:=xlAnd, VisibleDropDown:=False)
to: Call .AutoFilter(Field:=2, Criteria1:="<>", Operator:=xlAnd, VisibleDropDown:=True)

Hideblankrowsd()
change: Call .AutoFilter(Field:=1, Criteria1:="<>", Operator:=xlAnd, VisibleDropDown:=False)
to: Call .AutoFilter(Field:=1, Criteria1:="<>", Operator:=xlAnd, VisibleDropDown:=True)



Or....you could add column E add the following formula - =IF(AND(C2="",D2=""),"",IF(OR(C2="",D2=""),"","X")) 'or whatever value you want other than "X"
copy that down.

Expand Jon's tange to: Set rngToCheck = Excel.Intersect(.Columns("C:E"), .UsedRange)

and have only one call:
Call .AutoFilter(Field:=3, Criteria1:="<>", Operator:=xlAnd, VisibleDropDown:=False)

Multiple options there. :) I think they should all work
 
Upvote 0
No, not silly at all.
All part of the learning curve....(y)
 
Upvote 0
Excellent - all methods work to perfection.
However, one more senario.
Filter Columns C & D to only show rows with either a value in C, or D, or both.
In advance..thanks
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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