Macro to hide entire row if cell in column d of that row is empty

djclifton

New Member
Joined
Jul 26, 2016
Messages
13
I am trying to write a simple macro that will hide an entire row in in a worksheet if the cell in column D of that row is empty. Then unhide the entire row if the cell is then populated (by another macro .value).

I am absolutely useless at writing macros and any help would be awesome.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
THis will hide all the rows that are blank in "D"


I'd suggest you just incorporate this code into your other macro by putting this at the start of the other macro

Code:
Cells.EntireRow.Hidden = False'   will unhide the rows

with this further down in the code

Code:
Range("D1", Range("D" & Rows.Count).End(xlUp)).SpecialCells(4).EntireRow.Hidden = True 'will hide the blanks in "D"
 
Upvote 0
Here's how you might put Michael's nifty suggestions into a macro:

Code:
Option Explicit
Sub Macro1()
    
    Dim lngLastRow As Long
    
    lngLastRow = Cells(Rows.Count, "D").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
    With Range("D1:D" & lngLastRow)
        .EntireRow.Hidden = False 'Ensure all rows are unhidden
        .SpecialCells(4).EntireRow.Hidden = True 'will hide the blanks in "D1:D[lngLastRow]"
    End With
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Two problems I see with both codes as written - need tweaking.

1. If there are no empty cells the codes will error.

2. Both codes could miss hiding (or showing) rows

i) If the data looked like this, both codes will hide row 3 but not row 5

Excel Workbook
ABCDE
1Hdr 1Hdr 2Hdr 3Hdr 4Hdr 5
2xxxxx
3xxxx
4xxxxx
5xxxx
Hide Unhide



ii) If the data looked like this (note rows 3 and 5 hidden because blank) and the OP's other code now puts a value in cell D5, Robert's code will not unhide that row.

Excel Workbook
ABCDE
1Hdr 1Hdr 2Hdr 3Hdr 4Hdr 5
2xxxxx
4xxxxx
6
Hide Unhide





My suggestion assumes a heading row, and incorporating the following into the other macro (or triggered by Worksheet_Change)

Code:
With ActiveSheet
  If .FilterMode Then .ShowAllData
  Intersect(.UsedRange, .Columns("D")).AutoFilter Field:=1, Criteria1:="<>"
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,753
Messages
6,174,307
Members
452,554
Latest member
Louis1225

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