Note: The other languages of the website are Google-translated. Back to English

How to auto hide rows if blank cells in a column?

If you have a range of data which populate some blank cells in a column, and now, you want to hide the rows which contain the blank cells in that column automatically. Are there any good ways to solve this job in Excel quickly and easily?

Auto hide rows if blank cells in a column with VBA code


arrow blue right bubble Auto hide rows if blank cells in a column with VBA code

The following code may help you to hide all rows if blank cells in a specific column at once, and if you delete the cell content in that column, the rows will be hide automatically, too. Please do as follows:

1. Right click at the sheet tab that you want to auto hide the rows if there are blank cells in a column , and then choose View Code from the context menu, in the popped out Microsoft Visual Basic for Applications window, please copy and paste the following code into the blank Module:

VBA code: Auto hide rows if blank cells in a column:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice
    Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("A1:A20")
            If xRg.Value = "" Then
                xRg.EntireRow.Hidden = True
        
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True
End Sub

doc auto hide rows 1

Note: In the above code, A1:A20 is the data list which contains the blank cells you want to auto hide.

2. Then go back to the worksheet, and now, when you double click any cell and press Enter key, the rows which contain blank cells in column A have been hide at once, and if you clear any cell content in the specified cells of column A, the rows will be hide automatically.

doc auto hide rows 2

 


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom

 

Comments (30)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, i am using excel 2016 and it s not working. I get a sintax error message. can you help?
This comment was minimized by the moderator on the site
Hello, Ant,

The above code works well in my Excel 2016, have you change the cell reference in the code to your need?
This comment was minimized by the moderator on the site
I get a Run-time error ´13´:



Type mismatch



Someone who can help???
This comment was minimized by the moderator on the site
Can I stop this macro from running automatically after hitting enter? Could I attach it to a button to only hide the cells when I click?
This comment was minimized by the moderator on the site
did anyone ever figure this out? I too would like to know.
This comment was minimized by the moderator on the site
anyone come up with the answer yet?
This comment was minimized by the moderator on the site
same question here
This comment was minimized by the moderator on the site
Hello, guys,
To hide the rows which a specific column contains blank cells by using a button, please do as this:
First, you should insert a Command Button from the Developer tab.
And then apply the following VBA code for the Command Button, (Note: please change the cell reference a1:a20 to your own)

Private Sub CommandButton1_Click()
Dim rng As Range, x As Range
Set rng = Range("a1:a20")
Application.ScreenUpdating = False
For Each x In rng
If Len(x.Text) = 0 Then
x.EntireRow.Hidden = True
Else
x.EntireRow.Hidden = False
End If
Next x
Application.ScreenUpdating = True
End Sub

Please try it, hope it can help you!
This comment was minimized by the moderator on the site
how to Auto hide rows if blank cells in 2 different columns with VBA code
This comment was minimized by the moderator on the site
Hi, salpe,
To hide the rows if blank cells in 2 different columns automatically, please apply the following code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg, xCell As Range
Dim xRgs, xRgArea As Range
Set xRgs = Range("A1:A22,D1:D22")
Application.ScreenUpdating = False
On Error GoTo Ctn
For Each xRgArea In xRgs.Areas
Debug.Print xRgArea.Address
For Each xRg In xRgArea.Columns
For Each xCell In xRg.Rows
If xCell.Value = "" Then
xCell.EntireRow.Hidden = True
GoTo Ctn
ElseIf Not xCell.EntireRow.Hidden Then
xCell.EntireRow.Hidden = False
End If
Ctn:
Next
Next
Next
Application.ScreenUpdating = True
End Sub

You can change the cell references to your need.
Please try it!
This comment was minimized by the moderator on the site
بسم الله وجزاكم الله خيرا هذا الكود رائع ولكن حين قمت بتطبيق كود حذف الصف يبطأ الشيت بطريقة طويلة جدا فما العمل؟
This comment was minimized by the moderator on the site
I want to know whether the code will work If the cell is blank as a result of Formula ?
This comment was minimized by the moderator on the site
Hello, ACHINTA,
The above VBA code is also applied to the blank cells as a result of formula, you could try it, thank you!
This comment was minimized by the moderator on the site
The formula works to hide cells based on information, at the beginning, but does not re-reveal cells if their content changes. I want it to reveal any new cells that have been filled with information (which occurs via a cell lookup). How do I do this?
This comment was minimized by the moderator on the site
Bom dia, esta é minha necessidade também.

Preciso ocultar e quando a célula for preenchida (uso uma fórmula que preenche a mesma se houver valor em outra planilha) seja reexibida a linha novamente de forma automática.

Grato.
This comment was minimized by the moderator on the site
What I want is something like this to work in the active sheet:

If cells D2:D55 = ""
Then Hide.EntireRow

If cells D2:D55 = "has any value"
Then Show.EntireRow

How?
This comment was minimized by the moderator on the site
Hello, Kacha,You just need to change the cell reference in the code to your own cell range as below:<div data-tag="code">Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice
Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("D2:D55")
If xRg.Value = "" Then
xRg.EntireRow.Hidden = True

Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True
End SubAfter inserting the code, please remember to double click any cell and press Enter key to make the code take effect.
This comment was minimized by the moderator on the site
What is the simplest way to undo the auto-hide?
This comment was minimized by the moderator on the site
Hi, mình có dùng 2 code trong 1 file excel (1 code là của bạn) với mục đích in ra những bản riêng biệt và không bị thừa dòng trống. Khi mình ấn in từng bản thì ok nhưng nếu in 1 loạt thì code Hide không có tác dụng, bạn xem giúp mình với nhé, mình cảm ơn!

Sub PRINT_PRINT()
Dim rng As Range, x As Range
Set rng = Range("a16:a23")
Application.ScreenUpdating = False
For Each x In rng
If Len(x.Text) = 0 Then
x.EntireRow.Hidden = True
Else
x.EntireRow.Hidden = False
End If
Next x
Application.ScreenUpdating = True


Dim p1, p2, i&
p1 = Sheet1.Range("O1").Value
p2 = Sheet1.Range("O2").Value
If IsNumeric(p1) = False Or IsNumeric(p2) = False Then
tb = MsgBox("So code phai la so.", , "Thông báo")
Exit Sub
End If
If p1 > p2 Then
tb = MsgBox("So code sau phai >= so code truoc.", , "Thông báo")
Exit Sub
End If
If p1 < 1 Or p2 < 1 Then
tb = MsgBox("So code phai >= 1.", , "Thông báo")
Exit Sub
End If
If p1 <= p2 Then
For i = p1 To p2
Sheet1.Range("M2").Value = i
Sheet1.PrintOut
Next
End If
End Sub
This comment was minimized by the moderator on the site
Hola,Necesito crear una macro que me oculte una columna.Sería así: en la primera fila poner un 1 a las columnas que no quiero ocultar y no poner ningún valor a las columnas que quiero ocultar. Estoy haciendo el siguiente código, pero no se en que fallo:
Sub OcultarColumnaSin1()
Application.ScreenUpdating = False
For Each rango In Columns
If rango = "" Then
rango.EntireColumn.Hidden = True
Else
rango.EntireColumn.Hidden = False
End If
Next rango
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
This is working great for me, It hides the rows and opens them when my values change based on formulas.  The question I have is my sheet is 104 rows long.  It churns for a bit each time I click a cell.  Any way to speed up the transition when it is activated?  Or is it my computer?Thanks!
This comment was minimized by the moderator on the site
I would like to auto hide rows if column E is blank or 0
This comment was minimized by the moderator on the site
If the value in column E is blank or 0 i would like the row to auto hide. If the value in E changes to something other than blank or 0 I would like it to show. Report is 1500 rows
This comment was minimized by the moderator on the site
Hello, Cathy,
To hide the rows automatically based on blank cells or 0 values, please use the below vba code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice
    Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("E1:E1500")
            If (xRg.Value = "") Or (xRg.Value = "0") Then
                xRg.EntireRow.Hidden = True
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True
End Sub


Please try, hope it can help you!
This comment was minimized by the moderator on the site
How to hide row which contain checkbox?
This comment was minimized by the moderator on the site
Hello, Sweta

To hide rows with checkbox, the below article may do you a favor:
How To Hide Checkbox When Row Is Hidden In Excel?

Please try, if you still have any other question, please comment here.
This comment was minimized by the moderator on the site
When executing the code in office 2013/2019/2021 it works but takes too much time to complete (only 95 rows to hide).
How can you speed this?
Thanks!
This comment was minimized by the moderator on the site
Hi, Asaf,
The code works well in my Excel file, could you upload your attachment worbook here if you don't mind? So that we can help to check the problem.

Thank you!
This comment was minimized by the moderator on the site
apakah ada rumus lain?, saya berharap baris kosong itu akan terhapus saat mencetak file menjadi PDF tapi tampilan di excelnya tetap
This comment was minimized by the moderator on the site
Hello, Nurjanah
To solve your problem, first, you should hide the blank rows, and then print the data, after printing the data, you need unhide the blank rows again. Please do as this:
1. Apply this formula: =COUNTA(A2:E2) beside your data, see screenshot:
https://www.extendoffice.com/images/stories/comments/comment-skyyang/doc-hide-blanks-1.png
2. Then, filter the new helper Blank column, hide all 0 value rows, see screenshot:
https://www.extendoffice.com/images/stories/comments/comment-skyyang/doc-hide-blanks-2.png
3. And then, you should hide the new Blank column, and print the data, after printing the worksheet, please cancel the filter to unhide the blank rows as you need.
Please try, hope it can help you!
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations