excel error 1004 autofilter method of range class failed - vba

I am trying to use the autofilter method in a macro for some reason excel fails when calling the autofilter method. I get excel error 1004 autofilter method of range class failed. This is the line where it fails
Range("S2").Select
Range(Selection, Selection.End(xlDown)).AutoFilter Field:=1, Operator:= _
xlFilterValues, Criteria2:=Array(0, "1/0/1900")
This is already a changed version originally my code looked like this but I got the same error.
ActiveSheet.Range("$A$1:$W$" & Rows.Count).AutoFilter Field:=19, Operator:= _
xlFilterValues, Criteria2:=Array(0, "1/0/1900")
I tried this aswell but it also didn't work
ActiveSheet.Range("A:W").AutoFilter Field:=19, Operator:= _
xlFilterValues, Criteria2:=Array(0, "1/0/1900")

To the comment telling me the date is not valid. I am pretty sure it is. Thats how the date is entered in the worksheet I got from my coworker
[1]: https://i.stack.imgur.com/n1fDP.png
That's German Date Format but it is the same as 1/0/1990. I can not show more of the table cause of company rules

Related

Table can be filtered on date from the GUI, but not VBA

I have a table, where one of the columns is filled with dates. I want to add dynamic sorting on this list every time it is opened, so I made a macro to trigger a date filter update on Workbook_Open.
The problem is that the filter does not show any results when entered through VBA, even if I manually verify in the GUI that the code has entered the right dates. What's more, if I simply press Enter after verifying the dates in the GUI (that is, not changing anything), suddenly the filter works.
Here is the code, the out-commented lines are things I have tried. The last line is the EXACT code the macro-recorder gave me when I recorded the GUI-process (the manual process displaying the desired results while the code for it does not):
Sub FixSortings
tbl.Sort.SortFields.Clear
tbl.Sort.SortFields.Add Key:=Range("Tabell2[[#All],[Ref dato]]"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With tbl.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
tbl.ShowAutoFilter = False
'tbl.Range.AutoFilter Field:=9, Criteria1:=">=" & CStr(Format(DateAdd("m", -6, Now()), "dd.mm.yy")), Operator:=xlAnd, Criteria2:="<=" & CStr(Format(Now(), "dd.mm.yy"))
'tbl.Range.AutoFilter Field:=9, Criteria1:=">=" & CStr(Format(DateAdd("m", -6, Now()), "mm.dd.yy")), Operator:=xlAnd, Criteria2:="<=" & CStr(Format(Now(), "mm.dd.yy"))
'tbl.Range.AutoFilter Field:=9, Criteria1:=">=22.08.2016", Operator:=xlAnd, Criteria2:="<=22.02.2017"
ActiveSheet.ListObjects("Tabell2").Range.AutoFilter Field:=9, Criteria1:=">=22.08.2016", Operator:=xlAnd, Criteria2:="<=22.02.2017"
End Sub
The tbl declaration is correct, and I know that for a fact because the sort is added as should.. and the filters are added as well in some way (since I can find the entered dates when I inspect the table from the dropdown buttons in the GUI), it just seems that while the criteria for the filter are added, it isn't executed... or something of the sort.
I have an idea that it may be related to the localization of date formats. You can see in my code that I am using a European dateformat (dd.mm.yy), and I have previously had issues with datestamps in VBA because it seems that VBA prefers US date format.
However, as you can also see in my code, I have tried applying the mm.dd.yy format - with no success.
The answer seems to be that VBA is really particular about its dateformats.
This code works, when none of the ones in the question does.
tbl.Range.AutoFilter Field:=9, _
Criteria1:=">=" & CStr(Format(DateAdd("m", -6, Now()), "yyyy-mm-dd")), _
Operator:=xlAnd, _
Criteria2:="<=" & CStr(Format(Now(), "yyyy-mm-dd"))
As a closer, I can only conclude that I get no results using the previously tried code because VBA doesn't always play well with other dateformats, but it willingly entered the strings into the table anyway. When I then inspect the table's filter using the GUI and press Enter, Excel recognizes the dateformat because it's not as difficult as the VBA engine, and wham, filter applied successfully.

VBA Sort method of Range class failed

I know there are several threads on this topic but none of the answers have helped resolved this error.
I am trying to sort a table using three keys but receive the error, "Run-time error '1004': Sort method of Range class failed"
I've tried changing "Range("L2")" to ".Range("L2")" and received the error, "Compile error: Invalid or unqualified reference"
I've tried specifying the actual range instead of using columns, I've removed the last two keys and tried just the first, still received the run-time error.
shtData.Activate
shtData.Range(Range("A2"), Range("Z8000").End(xlUp)).Sort _
Key1:=Range("L2"), Order1:=xlAscending, _
Key2:=Range("M2"), Order2:=xlAscending, _
Key3:=Range("B2"), Order3:=xlAscending, _
Header:=xlYes
If you have any suggestions, I'd greatly appreciate it. I had this working yesterday and my excel crashed and did not recover the changes I made, I cannot figure out why I can't get it to work today.
you most probably have no data in "Z" column
if data rows range can be safely sized by column "A" not empty cells then go like follows
Option Explicit
Sub main()
Dim shtData As Worksheet
Set shtData = Worksheets("Data") '<--| some setting of 'shtData'
With shtData
.Range("Z2", .Cells(.Rows.Count, "A").End(xlUp)).Sort _
Key1:=.Range("L2"), Order1:=xlAscending, _
Key2:=.Range("M2"), Order2:=xlAscending, _
Key3:=.Range("B2"), Order3:=xlAscending, _
Header:=xlYes
End With
End Sub
If it is possible you could end up changing amount of columns again in the future, you could do something like.
With shtData
Range(Range("A2"), Cells(Range("A8000").End(xlUp).Row, Range("ZZ2").End(xlLeft).Column)
That way it will automatically size the sort area for however many columns you are using as well as rows.

Excel 2010 VBA Runtime 1004' PasteSpecial Method of class failed intermittently?

The below section of code works majority of the time but occasionally it will throw an error for
loc.PasteSpecial xlValues
with
Runtime 1004' PasteSpecial Method of class failed
I cant get it to regularly fault either so is hampering me tracking down the cause. Error doesn't happen when stepping through the code.
This method of copy and paste special is used a few times through out the macro.
Other answers for this question seem to be different use cases so I'm struggling to find the issue within my code.
'extracts the cfc data out to its own table if its there.
If Not IsError(Application.Match("CFC", rng1, 0)) Then
wb2.Sheets("Import").Activate
'reset filter to show all data
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
If ActiveSheet.AutoFilterMode = False Then ActiveSheet.Range("A1:BK1").AutoFilter
ActiveSheet.Range("A1:BK1").AutoFilter Field:=1,
'filters data based on criteria
Criteria1:=Array("*criteria1*"), Operator:=xlFilterValues
ActiveSheet.Range("A1:BK1").AutoFilter Field:=5, Criteria1:=Array("*CFC*"), Operator:=xlFilterValues
'Copys the rows that are visable.
Range("A2:BK" & ActiveSheet.UsedRange.Rows.Count + 1) _
.Cells.SpecialCells(xlCellTypeVisible).Rows.Copy
wb2.Sheets("CFC").Select
'reset filter to show all data
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
With Range("a:a") 'find the next available row on sheet data using column A
Set loc = .Find(What:="", LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
End With
outputrow = loc.Row 'sets the output row
loc.PasteSpecial xlValues
loc.PasteSpecial xlFormats
End If
Basic fuction is.
Autofilter data based on Criteria on the main data sheet
Copy remaining viable rows
Reset filtering on destination sheet
Find empty row at bottom of table
Paste copied rows to the found location.
I've been trying to sort this for a while now so any help would greatly appreciated.
Only copy the range immediately before you paste it (use a variable to store the range to copy). – Rory
As Rory commented. It was not placing the copy immediately before the paste that was causing the issues.
The copy code was changed to use a sheet reference variable and placed right before the paste and now the macro works as intended 100% of the time. :)
outputrow = loc.Row 'sets the output row
sh1.Range("A2:BK" & sh1.UsedRange.Rows.Count + 1) _
.Cells.SpecialCells(xlCellTypeVisible).Rows.Copy
loc.PasteSpecial xlValues
loc.PasteSpecial xlFormats

Excel VBA autofilter uncheck/exclude items

quick question, how can I exclude an item in a list through VBA. Have been working on a sheet that automatically prints out a list without a certain date on it.
Rows("2:2").Select
Selection.AutoFilter
ActiveSheet.Range("$A$3:$H$1000").AutoFilter Field:=3, Criteria1:="Hans"
ActiveSheet.Range("$A$3:$H$1000").AutoFilter Field:=7, Criteria1:="open"
ActiveSheet.Range("$A$3:$H$1000").AutoFilter Field:=6, Criteria1:="<>1/0/1900", Operator:=xlFilterValues
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Rows("3:3").Select
Selection.AutoFilter
Problem is that the criteria does not work with the date 0-1-1900 to filter it out. What am I doing wrong?
0-1-1900 is a date that does not exist. This might be the problem.
Just use
Criteria1:=">1/1/1900"
and it should word fine.

ActiveCell.Formula Run Time Error 1004

First off apologies for the unsightly formulae below - I'm not sure how I can make them easier to read without potentially removing required info for a solution.
I'm trying to automate some process in Excel, and am getting
run time error 1004
each time I try to step through the vba to populate the active cell with either of the following formulae. The formulae have been tested in Excel (I then recorded the entry and put into the vba).
1)
ActiveCell.FormulaR1C1 = _
"=16*POWER(SQRT(((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))*(1-((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))))/(((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))*((RC[-5]/RC[-6])-((SUMIFS(C[-5],C[-2],RC[-2],C[" & _
"1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6])))/((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))),2)"
2)
ActiveCell.FormulaR1C1 = _
"=IF(AND(IFERROR(IF(OR(NORMDIST((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))-(RC[-7]/RC[-8]))/SQRT(POWER(SQRT((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))*(1-((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8])))/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))),2)+" & _
"RT(((RC[-7]/RC[-8])*(1-(RC[-7]/RC[-8]))/RC[-8])),2)),0,1,TRUE)<(1-cpanel!R4C3),NORMDIST((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))-(RC[-7]/RC[-8]))/SQRT(POWER(SQRT((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC[-3])-RC[-8]))*(1-((SUMIFS(C[-7],C21,RC21,C22,RC[-3])-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,R" & _
"8])))/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))),2)+POWER(SQRT(((RC[-7]/RC[-8])*(1-(RC[-7]/RC[-8]))/RC[-8])),2)),0,1,TRUE)>cpanel!R4C3), ""YES"", ""NO""),""NO"")=""YES"",RC[-8]>RC[-2]),IF(RC[-1]=TRUE,""WINNER"",""LOSER""),""NOTSIG"")"
Is there any basic syntax that I'm misunderstanding here?
Thanks in advance for your time.