Hi, it has been a while since I have submitted a question to this forum. I wrote some Excel macro code which functions as expected for a particular circumstance. It filters a database report according to certain criteria in one column. Those rows that meet that criteria can then be deleted from the report. I then had a requirement to generalize this for filtering multiple columns according to different criteria in each case, so I came up with the following subroutine to be called within my macro (FindLastColumn and FindLastRow are user-created functions for finding the last column and row with data in a sheet):
It works for several different criteria arguments, however, when I call it like so
I get an error dialogue box telling me that "Autofilter method of Range class failed."
I read somewhere else online that VBA doesn't like you using the Array function in your arguments, so if you instead assign the Array function to a variable, like
then it may work. And I could have sworn that after I initially made this change it was working, but after some refactoring it does not anymore.
However, if I call it like this
(assuming that "Criterion1", "Criterion2" and "Criterion3" are actual values to be expected in the column) it doesn't complain and it does what I expect.
The wsQualSheet and wbReport objects are a worksheet and a workbook respectively (wsQualsheet is contained within wbReport, and both exist as named at runtime). Typing
in the Immediate Window indicates that it is of type Variant, and the supplied argument to the Array function is definitely a string, so I would expect compatibility with the Autofilter function. Looking at the values of all the other variables in the debugger indicates they are what is required, so what am I missing?
I can think of some more long-winded workarounds to this, but why does it not like the single argument "<>" passed in an array?
VBA Code:
Sub Filter_Delete(strColName As String, varCriteria As Variant, strShtName As String, strWbkName As String, Optional delCol As Boolean = False)
'Removes rows meeting criteria strCriteria from strColname in report sheet strShtName in workbook strWbkName and deletes strColname if delCol is true
Dim colFound As Boolean
Dim lngLastCol As Long, lngCol As Long
lngLastCol = FindLastColumn(strShtName, strWbkName)
lngLastRow = FindLastRow(strShtName, strWbkName)
colFound = False
For i = 1 To lngLastCol
If Workbooks(strWbkName).Sheets(strShtName).Cells(6, i).Value = strColName Then
colFound = True
lngCol = i
End If
Next i
Set rngData = Workbooks(strWbkName).Sheets(strShtName).Range(Workbooks(strWbkName).Sheets(strShtName).Cells(6, 1), _
Workbooks(strWbkName).Sheets(strShtName).Cells(lngLastRow, lngLastCol))
rngData.AutoFilter Field:=lngCol, _
Criteria1:=varCriteria, _
Operator:=xlFilterValues
rngData.Offset(1).Resize(rngData.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Workbooks(strWbkName).Sheets(strShtName).AutoFilterMode = False
If delCol Then Workbooks(strWbkName).Sheets(strShtName).Columns(lngCol).EntireColumn.Delete shift:=xlToLeft
End Sub
It works for several different criteria arguments, however, when I call it like so
VBA Code:
Call Filter_Delete("Qual Date", Array("<>"), wsQualSheet.Name, wbReport.Name, True)
I get an error dialogue box telling me that "Autofilter method of Range class failed."
I read somewhere else online that VBA doesn't like you using the Array function in your arguments, so if you instead assign the Array function to a variable, like
VBA Code:
arrCrit = Array("<>")
Call Filter_Delete("Qual Date", arrCrit, wsQualSheet.Name, wbReport.Name, True)
However, if I call it like this
VBA Code:
arrCrit = Array("Criterion1", "Criterion2", "Criterion3")
Call Filter_Delete("Qual Name", arrCrit, wsQualSheet.Name, wbReport.Name, True)
The wsQualSheet and wbReport objects are a worksheet and a workbook respectively (wsQualsheet is contained within wbReport, and both exist as named at runtime). Typing
VBA Code:
?typename(Array("<>"))
I can think of some more long-winded workarounds to this, but why does it not like the single argument "<>" passed in an array?