Excel 2013 VBA clear active filter - vba

I need to clear any active filters from a sheet before running a certain macro, this line works just fine IF there is an active filter on
If ActiveSheet.AutoFilterMode Then ActiveSheet.ShowAllData
However if no filters are selected it returns the error
Runtime error '1004';
ShowAllData method of Worksheet class failed
I got that code from an answer to this question
Excel 2013 VBA Clear All Filters macro
However that question doesn't explain how to ignore the line if no filters are active.
How do I ignore this line if there are no currently active filters applied?
EDIT
For example, all column headings have been auto filtered, so if my sheet is filtered by 'Female' for example I need to remove that filter before running the macro, however if no filters have been applied, just run the macro as normal

Use FilterMode instead of AutoFilterMode. I have dealt with filters frequently and this code works fine.
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Make sure the worksheet is not protected as this also gives the 1004 error.

I sincerely admire your desire to program for specific circumstances but I have to admit that the quickest way to accomplish this is with On Error Resume Next.
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
You shouldn't have to break something to check if it exists but in VBA that is occasionally the best recourse. Personally, I rank On Error Resume Next right up there with SendKeys as a programming method.
The above method does not require that you check to see if .AutoFilterMode is True.

I know this is a relatively old post and don't really like being a necromancer... But since I had the same issue and tried a few of the options in this thread without success I combined some of the answers to get a working macro..
Hopefully this helps someone out there :)
Sub ResetFilters()
On Error Resume Next
For Each wrksheet In ActiveWorkbook.Worksheets
wrksheet.ShowAllData 'This works for filtered data not in a table
For Each lstobj In wrksheet.ListObjects
If lstobj.ShowAutoFilter Then
lstobj.Range.AutoFilter 'Clear filters from a table
lstobj.Range.AutoFilter 'Add the filters back to the table
End If
Next 'Check next worksheet in the workbook
Next
End Sub
** Possible duplicate of thread: Excel 2013 VBA Clear All Filters Macro

Related

Sheet inaccessible to macro: Error 1004: Application/Object Defined Error

This is happening in several of my macros, but this is the one in from of me:
Private Sub resettool()
'''resets step 2 input and user input on MPP tabs
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Call showsheets 'makes all of these sheets .Visible = True
'clear data from lookups and data corrals
Sheets("Media by Copy Lookup").Range("b1",Range("b1").End(xlToRight).End(xlDown)).ClearContents
Sheets("Total Media Lookup").Range("d1",Range("d1").End(xlToRight).End(xlDown)).ClearContents
Sheets("Total Media Lookup").Range("b2:c100").ClearContents
Sheets("Media by Copy Data").Range("a1",Range("a1").End(xlToRight).End(xlDown)).ClearContents
'etc etc
End Sub
It continues with similar data-clearing lines for a while. This started happening when I took someone else's code and cleaned it by removing the .Select usages as people on here have suggested. It seems that the macro isn't able to access the sheets I'm referencing, because a line runs successfully if I step into the code, manually select the referenced sheet, and then hit go (but then of course I get the same error when I try to edit another sheet).
Any ideas why the macro wouldn't be able to access these sheets unless I explicitly activate/select them? The sheets are all visible, so that shouldn't be the problem.
P.S. I've seen the guide on using .Rows.Count).End(xlUp) instead of End(xlDown) to find the bottom of my data and will implement that soon, but this issue is occurring no matter how I define the range; it's about the sheet.

VBA stops running after filtering data, but with no error message

I am trying to filter data using the advanced filter, and then copy the visible cells to a separate sheet in the workbook:
Dim S_ALR, OUTPUTS, INPUTS As Worksheet
Set S_ALR = ActiveWorkbook.Sheets("S_ALR_87012357")
Set OUTPUTS = ActiveWorkbook.Worksheets("OUTPUTS")
'clear data
Worksheets("OUTPUTS").Range("A:AK").ClearContents
'copy Outputs from S_ALR_87012357 to OUTPUTS tab
S_ALR.Range("A:AK").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Sheets("FILTERS").Range("A3:B9"), Unique:=False
S_ALR.Range("A:AK").SpecialCells(xlCellTypeVisible).Copy
OUTPUTS.Cells(1, 1).PasteSpecial
However, after the data is filtered the macro stops with no error message.
I have commented out the filter and ran the macro & the rest of the code works properly. I have also tried incorporating the copy/past into the filter itself by putting CopyToRange in the filter, but that also hasn't worked.
I'm quite new to this, so it may be something simple that I am missing, but I'm absolutely stumped!
Please help.
Old question but thought I'd add an answer as I've just had the same issue and spent an hour looking into it. Turns out the fix seems to just be restarting Excel (closing down all Excel windows and reopening), and the AdvancedFilter line executed without issue for me after that.
For reference my line of code that exhibited the same behaviour was:
Range("B1:B1808").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
In response to the original question's comments:
Yes the code just stops, I stepped through my code, got to the Advanced Filter line, the AdvancedFilter line is then executed but the next line doesn't get stepped into, it's as if the AdvancedFilter line acted like an 'End' command
The AdvancedFilter line's execution succeeded, VBA just didn't continue onto the next line for some reason. I put a debug.print line after and it was never exectuted.
No Worksheet_Calculate event was being triggered, the workbook I was doing my testing in only had that single AdvancedFilter line of code (within a 'sub test' - 'end sub' of course)
While testing I explicitly added an 'On Error GoTo 0' before the AdvancedFilter line and this had no effect

Run-time error 1004 with Range.Autofilter in Excel VBA 2016

My code is
With ActiveSheet
.AutoFilterMode = False
.Range("23:23").AutoFilter
End With
This works fine in Excel 2010, but with Excel 2016 I get:-
Run-time error '1004'
AutoFilter method of Range class failed
Also, I can manually click the filter icon in the Ribbon (Under Data > Filter) but cannot do this with VBA code
Any ideas much appreciated.
The AutoFilter gives 1004 error, when you are trying to filter by an empty row. Try to put something on row 23 and to filter it again like this:
Public Sub TestMe()
With ActiveSheet
.AutoFilterMode = False
.Range("23:23").Cells(1) = 1
.Range("23:23").Cells(2) = 2
.Range("23:23").AutoFilter
End With
End Sub
If it works, then you simply do not have values in row 23, thus it cannot apply an autofilter.
In general, the AutoFilter in Excel has some strange behaviour. E.g., if you open a new Excel file and you run the following code:
Public Sub TestMe()
With ActiveSheet
.AutoFilterMode = False
'.Range("23:23").Cells(1) = 1
'.Range("23:23").Cells(2) = 2
.Range("23:23").AutoFilter
End With
End Sub
It will give you the 1004 error. Let's call this time momentum FirstTime.
Then, if you uncomment the two ranges and run it, the AutoFilter would appear.
Now the strange part - delete all cells from the sheet, comment back the two ranges and it really looks like the way it was, at the FirstTime. But if you run the code it will put an AutoFilter on the empty 23rd row without a problem.
Remove the Existing filer and Run it again

VBA Paste function throwing error after VBA Clear function

I have an advanced filter that is being used to sort a large data set.
The filter is dropping the filtered data into a separate sheet.
I have a VBA Macro that allows me to highlight the portions of the filter that I want to use and paste it into a range adjacent to the filter table.
Currently I am using very simple VBA.
The copy of the active selection and paste into the next open row after a specified Cell. The cell is a row of headers that corresponds to the headers of the table that the copy selection is being made from.
Sub CopyPaste()
Selection.Copy
ActiveSheet.Range("J6").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
End Sub
The clear is very simple.
Sub ClearTable()
ActiveSheet.Range("J7:O100").Clear
End Sub
After the clear is run, I receive an error.
Get Run Time error '1004, Application-defined or object defined error.
EDIT: Clarification. .clear and .clearcontents both lead to the errored state If I attempt to paste after I clear the range.
With J7:O100 cleared, select J6 and tap [ctrl]+[down arrow]. Try and go one more row further down. That is where you are trying to paste and if you follow my directions the problem should be perfectly obvious.
Use,
Selection.Copy Destination:=ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Offset(1, 0)
This looks from the bottom up and selects 1 row down not from the top down (which seems to be J1048576 and cannot be moved down a row let alone having room for the paste).

Excel VBA - Loop not adding autofilters to my worksheets

I have some code that (should) loop through all my worksheets and add an autofilter, however for some reason they're not showing up. When I turn on events, I can see that it is quickly being added then removed almost instantly. I'm assuming that this is because of something written earlier in my code, but I have too much code before this to evaluate what is causing the issue... Is there something I can add to guarantee the filters are added? Code:
Dim wsfixer As Worksheet
For Each wsfixer In ActiveWorkbook.Worksheets
With ActiveSheet
.AutoFilterMode = False
.Range("A:S").AutoFilter
End With
On Error Resume Next
Next
If you want to process the code on each worksheet, change
With ActiveSheet
to
With wsfixer
By using With ActiveSheet the code within the With block is being "shortcutted" to using the active sheet (e.g. .AutoFilterMode is treated as ActiveSheet.AutoFilterMode). So you are executing the same code over and over to the one active sheet.