Monday 16 September 2019

Excel - any way to auto hide empty columns when filtering rows?


I am working with data that's imported from a NOSQL database.


Sometimes I get worksheets with 1000 columns and up where most of the cells have no data in them.


I am looking for a way that when I filter the data and show only specific rows, all the columns for the visible rows which have no data at all will hide automatically.


This way, I will not have to horizontally scroll hundreds of empty columns looking for information.


If you have a solution for this need, I will be grateful.


Thanks,


Hanan Cohen



Answer



This code will hide columns if only the header cell is filled:


Sub KolumnHider()
Dim wf As WorksheetFunction
Dim i As Long, r As Range

Set wf = Application.WorksheetFunction
For i = 1 To 1000
Set r = Cells(1, i).EntireColumn
If wf.CountA(r) < 2 Then r.Hidden = True
Next i
End Sub


If there are no column headers, then make the 2 into a 1.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...