VBA & Macro Problems

Excel VBA: Hide Rows When an IF Formula Returns Blank

Nimra MalikNimra Malik Nov 12, 2026 869 views

Question details

The user needs to hide spreadsheet rows when an IF formula returns an empty string so printed letters do not contain large blank areas.

Product
Microsoft Excel with VBA
Device & OS
not provided
Scenario
A worksheet uses formulas and control cells to generate letters or documents, and rows with blank formula results should be excluded before printing.
Observed behavior
Rows remain visible even when their formulas return an empty string, leaving blank spaces in the printed output.
Before you start

Identify the control column or cells that determine whether each row should appear, and note the worksheet name and row range the macro must process. Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm) before adding VBA.

Solution 1Recommended

Use Excel VBA to hide rows with blank formula results

Use this solution when the worksheet must automatically hide rows based on an IF result or a control value such as No.

The macro can inspect the controlling cells and hide a row when the value is No or the formula result is an empty string. Change the worksheet name, control column, and row range to match your workbook.

1
Open the VBA editor

In Excel, open the workbook and press Alt+F11, then choose Insert > Module in the Visual Basic Editor.

2
Add the row-hiding macro

Paste a macro such as `Sub HideBlankRows() Dim ws As Worksheet Dim r As Long Set ws = ThisWorkbook.Worksheets("Sheet1") For r = 2 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ws.Rows(r).Hidden = (ws.Cells(r, "A").Value = "No" Or ws.Cells(r, "B").Value = "") Next r End Sub` and replace Sheet1, column A, and column B with the correct worksheet and columns.

3
Run the macro before printing

Return to Excel, select Developer > Macros, choose HideBlankRows, and click Run. Rows whose control value is No or whose formula result is blank will be hidden.

4
Unhide rows after printing

To restore the worksheet, select the affected rows, right-click the row headers, and choose Unhide. You can also create a second macro that sets `ws.Rows.Hidden = False` for the target range.

Formula results are checked as values: A formula that returns an empty string is treated as a blank value by the macro. If the formula returns spaces, use Trim in the condition, such as `Trim(ws.Cells(r, "B").Value) = ""`.
Free Microsoft Office alternative

Create clean, compatible spreadsheets and letters with WPS Office

WPS Office is a free, lightweight alternative for working with Excel and Word files. Its familiar interface supports common XLSX and DOCX workflows, making it easier to migrate spreadsheets, prepare data, and create polished documents without changing your file formats.

Open and edit Microsoft Excel XLSX workbooks with familiar spreadsheet tools.Work with Microsoft Word DOCX letters and document templates in the same office suite.Use a lightweight, easy-to-learn interface for spreadsheet and document tasks.Move between spreadsheet data and formatted letters with broad Microsoft Office format compatibility.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Excel not hide a row when an IF formula returns ""?

An IF formula that returns an empty string displays as blank, but Excel does not automatically change the row's Hidden property. A VBA macro or manual filtering step is required.

How can I hide rows when a control cell contains No?

Use a VBA condition such as `ws.Cells(r, "A").Value = "No"` inside a loop, then set `ws.Rows(r).Hidden = True` for matching rows. Update the column reference and row range for your sheet.

How do I show the hidden rows again after printing?

Select the affected row headers, right-click, and choose Unhide. A macro can also restore them by setting the target range's `Hidden` property to False.

Can Word Mail Merge avoid blank rows in generated letters?

Yes. Use Mailings > Rules > If...Then...Else to conditionally include or exclude text based on an Excel field. This keeps unwanted content out of the letter instead of hiding spreadsheet rows.