Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Macro Fails after AutoFilter.

Macro Fails after Filter

by Allen Wyatt
(last updated March 14, 2015)


In testing new macros that you develop, you may find that the macro fails when it is run and the information in a worksheet is filtered. This can happen because the macro may expect that all the information in the worksheet is available, or it may try to update information that is not visible on the screen.

The best solution to this problem is to make sure that the macro turns off filtering. This can be easily done by adding the following line of code near the beginning of the macro:

ActiveWorksheet.AutoFilterMode = False

This ensures that filtering is off and removes the problems that filtered data may present for your macro. (It will, of course, result in the loss of any filtering settings, but for the purposes of many macros that may be an acceptable side effect.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11083) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Macro Fails after AutoFilter.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Make AutoCorrect Pay Attention to Character Case

If you rely on AutoText (as most Word users do), you may have noticed that it doesn't always give the desired results ...

Discover More

Sharing Your Workbook

Need to allow others to contribute to your Excel workbook? It's easy to do if you just share it. This tip provides an ...

Discover More

Tabbing from One Table to the Next

Press the Tab key in a table, and Word dutifully moves to the next cell in that table. Press it in the last cell of a ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Displaying the Print Dialog Box in a Macro

Want to print a document by using a macro? One way is to display the Print dialog box and allow the user to interact with ...

Discover More

Automating Copying Macros

You can manually copy macros from one workbook to another, but what if you want to automate the copying process? Here's ...

Discover More

Opening a Workbook and Suppressing Automatic Macros

Want to stop Excel from running any automatic macros that may be stored with a workbook? Here's how to do it.

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 8?

2015-03-17 05:06:51


@Chuck Trese: thanks a lot for this code, will test now.

@balthamossa2b: thanks for your proposal, will check how this could work for me.

2015-03-17 00:48:42

Chuck Trese

Here is a pair of routines I use to capture and then later restore filter settings. As you can see in the comments, I originally got this from the Excel help files.

Sorry, the formatting might be awful in this comments section, but here it is:

'used to capture (and later re-apply) AutoFilter settings
Dim FilterArray()
Dim CurrentFiltRange As String
Dim FilterState As Boolean

Public Sub CaptureFilterSettings()
'captures all AutoFilter settings so they can be restored later
'(this routine was copied from the MS Excel help file, with slight modifications)

FilterState = ActiveSheet.AutoFilterMode 'remember if AutoFilter was on or off.
If FilterState = True Then 'if AutoFilter was on, then capture settings.
With ActiveSheet.AutoFilter
CurrentFiltRange = .Range.Address
With .Filters
ReDim FilterArray(1 To .Count, 1 To 3)
For f = 1 To .Count
With .Item(f)
If .On Then
FilterArray(f, 1) = .Criteria1
If .Operator Then
FilterArray(f, 2) = .Operator
FilterArray(f, 3) = .Criteria2
End If
End If
End With
Next f
End With
End With
End If

End Sub

Public Sub RestoreFilterSettings()
'restores all AutoFilter settings that were captured earlier (by CaptureFilterSettings)
'(this routine was copied from the MS Excel help file, with slight modifications)

If FilterState = True Then 'if AutoFilter was on (when CaptureFilterSettings was called)
'restore previous AutoFilter settings
ActiveSheet.AutoFilterMode = False
For col = 1 To UBound(FilterArray(), 1)
If Not IsEmpty(FilterArray(col, 1)) Then
'restore filter settings for this column
If FilterArray(col, 2) Then
ActiveSheet.Range(CurrentFiltRange).AutoFilter Field:=col, _
Criteria1:=FilterArray(col, 1), _
Operator:=FilterArray(col, 2), _
Criteria2:=FilterArray(col, 3)
ActiveSheet.Range(CurrentFiltRange).AutoFilter Field:=col, _
Criteria1:=FilterArray(col, 1)
End If
End If
If ActiveSheet.AutoFilterMode = False Then 'if AutoFilter is still off (because no filters were set), then
ActiveSheet.Range(CurrentFiltRange).AutoFilter 'turn AutoFilter back on anyway (even though no filters are set)
End If
End If 'filterState = True (i.e., AutoFilter was on)

End Sub

I think I wrote this when I was using Excel 2007. However, I am now using it on Excel 2010 with no problems.

2015-03-16 12:35:00


To avoid filtering troubles just do the filtering yourself through a macro, with


That way the filtered rows are still readable by the macro.

2015-03-16 03:55:19


Quite rightly you said "... It will, of course, result in the loss of any filtering settings, ..."
Wouldn't it be great if there was a piece of code that could re-instate the original filter settings as last step of a macro?
Any idea how to do that?

2015-03-14 16:55:57

Marcel Kollenaar

I had the same issue when I opened a workbook and needed the data on a sheet with an active filter. I found out that if I use ".usedrange" I got the raw data without filter.

In this case you do not alter filter settings. However, your range may be greater than the data you expect.

This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.