You are here:

Excel/Excel Autofilter Macro


Hi Tom,

I've read some of your responses and honestly I'm not that good with excel, but I'm pretty sure you can help me. I'm trying to write a really simple Macro that will hide entire columns that are completely empty after an auto filter selection has been made, but I need the range to hide, to be set to the range of the auto filter after a selection has been made.

Currently I have:

Dim rng As Range
Set rng = AutoFilter.Range (not sure how to do this part)

For Each c In rng
If c.Value = "" Then c.EntireColumn.Hidden = True

End Sub



the following is untested, but should represent an approach.

Sub HideColumns()
Dim rng As Range
Dim c as Range
Set rng = Activesheet.AutoFilter.Range
rng.Entirecolumn.Hidden = False
For Each c In rng.columns
' use the countA functionality to count filled cells
' assume the header row will be filled and visible
if application.Subtotal(3, c) < 2 then
   c.EntireColumn.Hidden = True
end if

End Sub

Post back if you have issues with it or send a sample file to
including telling me what version of excel you are using.
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 All rights reserved.