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)

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

MORE FROM ALLEN

Automatically Adding Captions

Word can automatically add captions to various elements of your document, such as tables or figures. Here's how to ...

Discover More

Creating a Center Across Selection Button

The ability to center text across a range of cells has long been a staple of experienced Excel users. Here's how to ...

Discover More

Keyboard Shortcuts for Zooming

For whatever reason, Microsoft has never seen fit to provide built-in keyboard shortcuts zooming in and out. Here are ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Magnifying Only the Current Cell

You can use the Zoom feature of Excel to magnify what Excel shows of your workbook, but it affects the entire screen. ...

Discover More

Using Seek In a Macro

When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...

Discover More

Tools on Developer Tab are Unavailable

Want to add some macros to your workbook? What do you do if you try to add the macros but the program has disabled the tools?

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 8 + 2?

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.