Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. 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 February 6, 2020)

5

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, 2013, 2016, 2019, and Excel in Office 365. 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. ...

MORE FROM ALLEN

Hyperlinking to a Specific Excel Worksheet

Creating a hyperlink to an Excel workbook is easy. With the information in this tip you can discover how to hone that ...

Discover More

Multiple Print Areas on a Single Printed Page

Want to print small, non-contiguous areas of your worksheet all on a single page? You might think that defining a ...

Discover More

Freezing Top Rows and Bottom Rows

Freezing the top rows in a worksheet so that they are always visible is easy to do. Freezing the bottom rows is not so ...

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)

Determining an Integer Value

When creating macros, you often need to process numbers in various ways. VBA allows you to convert a numeric value to an ...

Discover More

Unprotecting Groups of Worksheets

Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can ...

Discover More

Debugging a Macro

Part of writing macros is to make sure they work as you expect. This involves a process known as debugging. Here's how ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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

2015-03-17 05:06:51

MichaelR

@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

MichaelR,
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)
Else
ActiveSheet.Range(CurrentFiltRange).AutoFilter Field:=col, _
Criteria1:=FilterArray(col, 1)
End If
End If
Next
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

balthamossa2b

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

Range.EntireRow.Hide

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


2015-03-16 03:55:19

MichaelR

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
Subscribe

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.