Hello lucausa75
Welcome to Microsoft Community.
According to your current description, it seems that VBA code in older versions of Excel 2013 will have a run-time error '1004' when running in new versions of Excel 365.
The issue might be caused by the way the Criteria1 parameter is passed to the AutoFilter method. Instead of passing an array of strings as Criteria1, you can try passing a comma-separated string of values enclosed in double quotes.
| For example, replace this line:<br><br><br><br><br> ActiveWorkbook.Sheets("PivotStock").Range("$A:$D").AutoFilter Field:=4, Criteria1:=MyArray, Operator:=xlFilterValues <br><br><br><br><br>with this line:<br><br><br><br><br> ActiveWorkbook.Sheets("PivotStock").Range("$A:$D").AutoFilter Field:=4, Criteria1:=Join(MyArray, ","), Operator:=xlFilterValues |
|---|
This should concatenate the values in the MyArray variable into a string separated by commas, which can then be used as the Criteria1 parameter in the AutoFilter method.
Alternatively, you can also try passing the Criteria1 parameter as a variant array instead of a string array. To do this, declare the MyArray variable as a variant instead of a string array, and use the Transpose function to convert the vertical array into a horizontal array.
| For example, replace this line:<br><br><br><br><br> ReDim MyArray(1 To LastRowIndexForPivot - 3) <br><br><br><br><br>with this line:<br><br><br><br><br> Dim MyArray As Variant <br><br><br><br> MyArray = Application.Transpose(ActiveSheet.Range("D4:D" & LastRowIndexForPivot).Value) |
|---|
| Then, replace this line:<br><br><br><br><br> ActiveWorkbook.Sheets("PivotStock").Range("$A:$D").AutoFilter Field:=4, Criteria1:=MyArray, Operator:=xlFilterValues <br><br><br><br><br>with this line:<br><br><br><br><br> ActiveWorkbook.Sheets("PivotStock").Range("$A:$D").AutoFilter Field:=4, Criteria1:=Array(MyArray), Operator:=xlFilterValues |
|---|
This should pass the Criteria1 parameter as a variant array, which might work better with the AutoFilter method. Here's an updated version of the VBA code that should work in Excel 365:
Sub FilterGreaterThanZero()
Dim ws As Worksheet
Dim LastRowIndexForPivot As Long
Dim MyArray As Variant
Dim Cont As Long
Dim i As Long
Set ws = ActiveWorkbook.Sheets("PivotStock")
LastRowIndexForPivot = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
' Store values greater than zero in an array
ReDim MyArray(1 To LastRowIndexForPivot - 3)
Cont = 0
For i = 4 To LastRowIndexForPivot
If ws.Cells(i, 4).Value > 0 Then
Cont = Cont + 1
MyArray(Cont) = CStr(ws.Cells(i, 4).Value)
End If
Next i
' Apply filter
ws.Range("$A:$D").AutoFilter Field:=4, Criteria1:=Join(MyArray, ","), Operator:=xlFilterValues
' Clear selection
ws.Range("A1").Select
End Sub
This code stores the active worksheet in a variable (ws), uses the End(xlUp) method to find the last row index for the pivot table, and clears the selection after applying the filter.
Please note that the filter might not work as expected if there are blank cells or errors in the column being filtered. Feel free to let me know how it goes.
Best regards,
Chandy |Microsoft Community Support Specialist