Hide blank cells in a table

kalim

Board Regular
Joined
Nov 17, 2010
Messages
87
Hi Excel users.

I have these two macros that will hide/unhide blank cell in a table.
What I am tryng to do is combine them so the user only needs to click one button and not two.

Code:
Sub Hide_blank_rows()
 
ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=2, Criteria1:= _
"<>"
End Sub
 
Sub Show_blank_rows()
 
    ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=2
End Sub

I have tried with an if statatement, but I get an error message.

Code:
Sub Hide_show_blank_rows()
 
if ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=2 then
 
ActiveSheet.Shapes("Button 2").TextFrame.Characters.Text = "Unhide"
ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=2, Criteria1:= _
"<>"
 
Else
 
ActiveSheet.Shapes("Button 2").TextFrame.Characters.Text = "Hide"
ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=2
 
    End If
End Sub

Any suggestions?

Thanks.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi kalim,

This code assumes that "Table 2" begins in column A, for this I've defined a range called "Table2" from A1:A21,
even when the whole table goes from A1:E21. You could use Columns("A:A") or Range("A1:A21") instead of
Range("Table2"). In this case is not needed to define range "Table2".

Code:
Sub Hide_Unhide()
[COLOR=Green]'Hl=Hidden lines[/COLOR]
    Hl = Range("Table2").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = False
    
    If Hl = True Then
        Range("Table2").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
    Else
        Range("Table2").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = False
    End If
End Sub
It can be adapted to your needs, if not works for you, please show a sample of your sheet.

Hope this helps.

Regards
.
 
Upvote 0
Code:
Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects("Table2")
With tbl
    If .AutoFilter Is Nothing Then
        MsgBox "Filters are not enabled."
    ElseIf .AutoFilter.Filters(2).On = False Then
        .Range.AutoFilter Field:=2, Criteria1:="<>"
    Else
        .Range.AutoFilter Field:=2
    End If
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,722
Members
452,939
Latest member
WCrawford

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