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

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

Related

Dynamic ActiveCell address in Excel VBA

Can someone help me debug this VBA macro?
I want the macro to set a value in a cell using a relative address. If the value in ActiveCell is "Primary", I want the macro to put the value "1.000" in the cell immediately to the right of the ActiveCell.
Sub SetSplitPercent()
If Worksheets("Reps To Users").ActiveCell.Value = "Primary" Then
col = ActiveCell.Column + 1
Cells(ActiveCell.Row, col) = "1.000"
End If
End Sub
The above macro throws the error "Application-defined or object-defined error" with the Cells() statement being the offending one.
Thanks in advance for the help.
I've tried several different approaches and haven't been able to get any of them to work without error. This approach seems like the closest I've gotten but still no dice.

Change range formula to include multiple if statements VBA

Bonjour!
I am trying to fill some cells with If functions.
In excel this would look like this:
=if(B11="Apple"; "nice";if(B11="Banana";"Also nice";"why?")
If I only use the first part of the if statement the code runs without problem
rng.formula = "=if(B11=""Apple"", ""Nice"", ""why?"")"
However, as soon as I add another if I get an
Run Time Error '1004' Application defined or object-defined error
rng.Formula = "=if(B11=""Apple"",""Nice"",if(B11=""Banana"",""Also nice"",""why?"")"
How can I include several
You are missing a parens:
Sub dural()
Set Rng = ActiveCell
Rng.Formula = "=if(B11=""Apple"",""Nice"",if(B11=""Banana"",""Also nice"",""why?""))"
End Sub

Run time Error - 438

I have the following code in which I am trying to copy data from one sheet to another in same workbook. When I run the code I get Runtime error -438
Sub Copy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet2").Activate
Range("E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Try the following code. You should not rely on Activate and Select.
Sub ZCopy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet1").Paste Destination:=Worksheets("Sheet2").Range("E1")
Application.CutCopyMode = False
End Sub
Interesting Reads
MSDN
How to avoid using Select in Excel VBA macros
Do you have a particular need for copy and paste? This can be slow and inefficient. If you're just copying data from one sheet to another, you can set the values of one range equal to the values of another range and avoid the whole thing.
Sheets("Sheet2").Range("E1:H20").Value = Sheets("Sheet1").Range("A1:D20").Value
This will set the range from cells E1:H20 on Sheet2 to the same values as those in the range A1:D20 on Sheet1, which is effectively a copy and paste. I should add that this will work only for the values themselves.
If there is specific formatting (or formulas) that you need copied and pasted, this method won't work.

Excel 2013 VBA clear active filter

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

Repeated calls of Chart.SetSourceData give error 1004

I have a problem with an application that was created in Excel 2003 in my company. The application retrieves data from a source and updates a Chart using the SetSourceData in a VBA routine passing a Range containing the cells where the relevant data is written.
The application runs just fine in Office 2003, but when the application is executed in Office 2010 it gives this error:
Run-time error '1004': Method 'SetSourceData' of object'_Chart' failed.
I have created a For loop in a simple Excel file in Office 2010 and depending on the number of columns passed in the Range to the Chart the error will come up sooner or later. The more columns passed in the Range the sooner it will come up. I guess this has to be related with the number of series in the Chart(more columns more series).
Is this some sort of mechanism/buffer in the Chart Object or Series implemented in Office 2010 that did not exist in Office 2003? The same For loop never shows a problem when it is run in Office 2003 and I am not sure how to solve this problem.
So far I have only been able to delete all the Series controlling the Error with a Goto instruction to delete all the series in the SeriesCollection using a For Each loop to select all the objects in the SeriesCollection of the Chart. If I do this and resume the execution of the application when I pass the Range again all the data is painted in the Chart Object properly.
Example to reproduce the error. The following code is to be put in a VBA module in a new Excel 2010 workbook. Run the Sub setDataChart and the application will run until the error message is displayed.
Sub setDataChart()
Call createAColValues
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=Range("A1:FA6"), PlotBy:=xlColumns
ActiveSheet.ChartObjects(1).Activate
With ActiveChart.Parent
.Height = 325
.Width = 900
.Top = 120
.Left = 10
End With
Call updateValues
Call sendData
End Sub
Sub sendData()
Dim cht As ChartObject
Set cht = ActiveSheet.ChartObjects(1)
'On Error GoTo delSeries:
For i = 0 To 1000
cht.Chart.SetSourceData Source:=ActiveSheet.Range("A1:FA6"), PlotBy:=xlColumns
Next i
End Sub
Sub createAColValues()
Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Range("A2").Select
ActiveCell.FormulaR1C1 = "2"
Range("A1:A2").Select
Selection.AutoFill Destination:=Range("A1:A6"), Type:=xlFillDefault
Range("A1:A6").Select
End Sub
Sub updateValues()
Range("B1").Select
ActiveCell.FormulaR1C1 = "=RANDBETWEEN(0,10)"
Range("B1").Select
Selection.AutoFill Destination:=Range("B1:B6"), Type:=xlFillDefault
Range("B1:B6").Select
Selection.AutoFill Destination:=Range("B1:FA6"), Type:=xlFillDefault
Range("B1:FA6").Select
End Sub
This doesn't address why the error occurs. This is a workaround.
Before calling SetSourceData, delete all the existing series currently in the chart, and the code will run as expected.
For j = cht.Chart.SeriesCollection.Count To 1 Step -1
cht.Chart.SeriesCollection(j).Delete
Next j
I'm not sure why the error occurs in the first place, but this makes it go away.
Another possibility is to define a named range for the data that's defined using the Offset formula and appropriate reference cells. This requires the data to be contiguous, not change the initial row & column it begins in, and for you to setup at least one reference formula (=COUNTA() on the column/row containing the data) that can be used to set the height/width of the offset range.
Otherwise a very handy little work around to take this out of macros and put it in worksheet logic.