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. ...


Sending Drawing Objects to the Back or Front

Drawing objects can be placed in a document in such a way that they overlap with each other. If you want to arrange those ...

Discover More

Printing Show/Hide Characters

Non-printing characters are very handy to view when editing a document. But what if you want those characters to no ...

Discover More

Excel Serious Sorting (Special Offer)

Excel Serious Sorting shows you how to put your data into the order that makes best sense. Increase your ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Using SUM In a Macro

Want to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.

Discover More

Calculating the Distance between the Top of the Window and Row 1

Normally Excel positions a UserForm in the center of your screen. You may want to position the form elsewhere, more ...

Discover More

Changing Macro Cell References Based on Edits

Place a cell reference in a macro, modify the structure of your worksheet, and you may soon find that the cell reference ...

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 1 + 3?

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.