Hide/unhide empty rows and columns with VBA

hjs1508

New Member
Joined
Feb 28, 2018
Messages
2
In my Worksheet2, the data are generated based on formulas that reference to data in Worksheet1, and are formatted as a list (from A1 to AS100) like below:

ABCDEF...AS
1IDCodeProperty1Property2Property3Property4...Property28
2
1AAA111
3
43CCC222
54DDD222
6
......
10099ZZZ999

<tbody>
</tbody>

Some rows and columns (except the header row) in this worksheet are empty, e.g. Row 3, Row 6, Column E, Column F.
I would like to auto hide and unhide both empty rows and columns, preferably with a button.
Any assistance or suggestions would be greatly appreciated. Thank you.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
try this:

Code:
Sub blabla()
    For i = 1 To 100
    
        If Application.CountA(Rows(i)) = 0 Then Rows(i).Hidden = True
        If Application.CountA(Columns(i)) = 1 Then Columns(i).Hidden = True
    Next
End Sub
 
Upvote 0
Thanks Vbagreenhorn1!
Somehow the code works quite strangely.
In my actual worksheet2, there are only data from cell A1 to AS1(the headers) and A2 to N7, other cells are blank, i.e. ="" (based on the formula set).
After running the code, no rows are hidden, and columns hidden are P, S, V, Y, AB, AE, AH, AK, AN and AQ.

try this:

Code:
Sub blabla()
    For i = 1 To 100
    
        If Application.CountA(Rows(i)) = 0 Then Rows(i).Hidden = True
        If Application.CountA(Columns(i)) = 1 Then Columns(i).Hidden = True
    Next
End Sub
 
Upvote 0
Range("Q2").Select
ActiveCell.FormulaR1C1 = "=COUNTA(RC[-15]:RC[-2])"
Range("Q2").Select
Selection.AutoFill Destination:=Range("Q2:Q33"), Type:=xlFillDefault
Range("Q2:Q33").Select
ActiveWindow.SmallScroll Down:=6
Range("B40").Select
ActiveCell.FormulaR1C1 = "=COUNTA(R[-38]C:R[-7]C)"
Range("B40").Select
Selection.AutoFill Destination:=Range("B40:O40"), Type:=xlFillDefault
Range("B40:O40").Select

Dim c As Range, rng
Set rng = Range("b40:eek:40")
For Each c In rng
If c.Value = 0 Then
c.EntireColumn.Hidden = True
End If
Next c
Set rng = Range("q2:q33")
For Each c In rng
If c.Value = 0 Then
c.EntireRow.Hidden = True
End If
Next c
Range("q2:q33").ClearContents
Range("b40:bo40").ClearContents
 
Upvote 0
Hi Steve,

Would your routine be able to check, in a specified range, whether a cell in a specified row has a value of "0" and then autohide a row if that is true?

Thanks in advance
Chris
 
Upvote 0

Forum statistics

Threads
1,216,122
Messages
6,128,963
Members
449,480
Latest member
yesitisasport

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