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.
by Allen Wyatt
(last updated June 6, 2020)
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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Do you need to create a number of words or phrases where you only alter a few letters in each one? If the alterations ...Discover More
The VBA programming language provided with Excel allows you to create and modify text files quite easily. Here's how to ...Discover More
Having problems with using macros in a protected workbook? There could be any number of causes (and solutions) as ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.