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

Compound Page Numbering

Simple page numbering is easy to add to your documents. More complex numbering (such as two numbering schemes in the same ...

Discover More

Error in Linked PivotTable Value

Excel allows you to link to values in other workbooks, even if those values are in PivotTables. However, Excel may display an ...

Discover More

Counting Dates in a Range

Excel makes working with a list of dates relatively easy. If you have a list of dates, you may need to know how many of those ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

Discover More

Finding Workbooks Containing Macros

Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might want ...

Discover More

Finding Other Instances of Excel in a Macro

When processing information using a macro, you may need to know if there are any other instances of Excel running on a ...

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 four less than 9?

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.