How to hide rows if cells are empty

hond70

Board Regular
Joined
Apr 13, 2004
Messages
72
Hi there,

Could somebody help me to write code for a macro that hides all rows in which a certain cell is empty:

Column A - Column B - Column C
1 Philips - 1000000 - 200 mln
2 Shell
3 Lever
4 Nutricia - 5400000 - 240 mln

I would like a macro that hides row 2 (and 3) when B2 (and B3) are empty.
Also I need a macro that hides row 2 (and 3) when range B2:C2 (and B3:C3) are empty.

Next I need a macro that is a updater (looks if hidden rows are not empty anymore and unhides them

The last macro unhides all hidden rows whether or not the cell is empty.

Please advice! Thanks in advance.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Note that these are untested:

Macro 1:
Code:
sub macro1()
range("b2").select
do while activecell.row < 100
if activecell.value = "" and activecell.offset(0,1).value = "" then
Selection.EntireRow.Hidden = True
endif
activecell.offset(1,0).select
loop
end sub

Macro 2:
Code:
sub macro1()
range("b2").select
do while activecell.row < 100
if activecell.value = "" and activecell.offset(1,0).value = "" then
Selection.EntireRow.Hidden = True
endif
activecell.offset(1,0).select
loop
end sub

Macro 3:
Code:
Cells.Select
    Selection.EntireRow.Hidden = False

I hope that this helps
:biggrin:
 
Upvote 0
Wow, the first and the last macros work like a charm :pray:

Unfortunately, the second one (the updater) doesn't work. It supposed to check all cells again and hide only the empty ones, or in other words: it supposed to check all cells again (F9) and unhide cells that now contain data.

Thanks anyway! This is great.
 
Upvote 0
You should be able to get away with just these two...
Code:
Sub Updater()
Cells.EntireRow.Hidden = False
For Each cell In Range("B2", Range("B65536").End(xlUp))
If cell = "" And cell.Offset(, 1) = "" Then cell.EntireRow.Hidden = True
Next cell
End Sub
What it does is unhides all rows, checks to see if the cells in columns B & C are blank and if so, re-hides just those rows.
And
Code:
Sub UnhideAll()
Cells.EntireRow.Hidden = False
End Sub
This what you're looking for?
 
Upvote 0
Alternatively, for macro 2:

sub macro2()
Cells.Select
Selection.EntireRow.Hidden = False
range("b2").select
do while activecell.row < 100
if activecell.value = "" and activecell.offset(1,0).value = "" then
Selection.EntireRow.Hidden = True
endif
activecell.offset(1,0).select
loop
end sub

You can also add those 1st 2 lines into macro 1.

Regards
 
Upvote 0
Provided that the sheet's used range always extends past column B, it could be done with a "one-liner" (no loop) :-

Code:
Sub Updater()
Cells.EntireRow.Hidden = False
On Error Resume Next
Range([A1], [A65536].End(xlUp)).Offset(, 1).SpecialCells(xlCellTypeBlanks).Offset(, 1).SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
On Error GoTo 0
End Sub

Note : This assumes that you are looking to hide cells that are blank, and not cells that contain "" as a result of a formula.
 
Upvote 0
Hiding rows when blank

But how do you modify this such that it only hides rows between a specific range (say a50 through to a100) which are empty.

I have a worksheet where the central part is populated through lookup formula between rows 50 and 100 and I want to blank out the rows in that range where the lookup returns no results

Any ideas??
 
Upvote 0
Ok..What if columns range is more than 10. then can we hide entire row if there is no data in cell.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,329
Messages
6,124,302
Members
449,150
Latest member
NyDarR

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