O
OssieMac
This post looks long but there is really only one line of the code giving
problems and the remainder is purely to provide the full example so I hope
someone can help me with this and point out what I am missing here.
I can record code while setting a list of numeric values in AutoFilter and I
can then return the ValueList with code as per the following example (works
fine):-
Sub ValueListFilterNumbers()
Dim arrCriteria1()
Dim i As Long
'Recorded code to filter on valuelist of numbers
'Criteria1 is an array
ActiveSheet.Range("$A$1:$D$185") _
.AutoFilter Field:=1, _
Criteria1:= _
Array("1", "3", "5", "7"), _
Operator:=xlFilterValues
'Assign Criteria1 to an array
With ActiveSheet.AutoFilter.Filters(1)
arrCriteria1() = .Criteria1
End With
'Read the values in the array
For i = 1 To UBound(arrCriteria1)
MsgBox arrCriteria1(i)
Next i
End Sub
If I record code as per the following example while setting a list of dates
then it appears that the array is assigned to Criteria2. I thought that I
should be able to assign Criteria2 to an array similar to Criteria1 above but
it does not work and I get a runtime error 1004. (See code between asterisk
lines)
Sub ValueListFilterDates()
Dim arrCriteria2()
Dim i As Long
'Recorded code to filter on valuelist of dates
'Criteria2 is an array
ActiveSheet.Range("$A$1:$D$185") _
.AutoFilter Field:=2, _
Operator:=xlFilterValues, _
Criteria2:=Array(2, "10/1/2009", _
2, "10/3/2009", _
2, "10/5/2009", _
2, "10/7/2009", _
2, "10/9/2009")
'Assign Criteria2 to an array
With ActiveSheet.AutoFilter.Filters(2)
'******************************************
'This code returns runtime error 1004
arrCriteria2() = .Criteria2
'******************************************
End With
'I want to do the following but without the previous
‘code it obviously will not work.
‘Read the values in the array
For i = 1 To UBound(arrCriteria2)
MsgBox arrCriteria2(i)
Next i
End Sub
The following test shows that the array on its own from the above code can
be assigned to an array variable so it is not a problem mixing numeric and
dates. The problem seems to be with assigning .Criteria2 to an array.
Sub arrayTest()
Dim test()
Dim i As Long
test() = Array(2, "10/1/2009", _
2, "10/3/2009", _
2, "10/5/2009", _
2, "10/7/2009", _
2, "10/9/2009")
For i = 0 To UBound(test)
MsgBox test(i)
Next i
End Sub
problems and the remainder is purely to provide the full example so I hope
someone can help me with this and point out what I am missing here.
I can record code while setting a list of numeric values in AutoFilter and I
can then return the ValueList with code as per the following example (works
fine):-
Sub ValueListFilterNumbers()
Dim arrCriteria1()
Dim i As Long
'Recorded code to filter on valuelist of numbers
'Criteria1 is an array
ActiveSheet.Range("$A$1:$D$185") _
.AutoFilter Field:=1, _
Criteria1:= _
Array("1", "3", "5", "7"), _
Operator:=xlFilterValues
'Assign Criteria1 to an array
With ActiveSheet.AutoFilter.Filters(1)
arrCriteria1() = .Criteria1
End With
'Read the values in the array
For i = 1 To UBound(arrCriteria1)
MsgBox arrCriteria1(i)
Next i
End Sub
If I record code as per the following example while setting a list of dates
then it appears that the array is assigned to Criteria2. I thought that I
should be able to assign Criteria2 to an array similar to Criteria1 above but
it does not work and I get a runtime error 1004. (See code between asterisk
lines)
Sub ValueListFilterDates()
Dim arrCriteria2()
Dim i As Long
'Recorded code to filter on valuelist of dates
'Criteria2 is an array
ActiveSheet.Range("$A$1:$D$185") _
.AutoFilter Field:=2, _
Operator:=xlFilterValues, _
Criteria2:=Array(2, "10/1/2009", _
2, "10/3/2009", _
2, "10/5/2009", _
2, "10/7/2009", _
2, "10/9/2009")
'Assign Criteria2 to an array
With ActiveSheet.AutoFilter.Filters(2)
'******************************************
'This code returns runtime error 1004
arrCriteria2() = .Criteria2
'******************************************
End With
'I want to do the following but without the previous
‘code it obviously will not work.
‘Read the values in the array
For i = 1 To UBound(arrCriteria2)
MsgBox arrCriteria2(i)
Next i
End Sub
The following test shows that the array on its own from the above code can
be assigned to an array variable so it is not a problem mixing numeric and
dates. The problem seems to be with assigning .Criteria2 to an array.
Sub arrayTest()
Dim test()
Dim i As Long
test() = Array(2, "10/1/2009", _
2, "10/3/2009", _
2, "10/5/2009", _
2, "10/7/2009", _
2, "10/9/2009")
For i = 0 To UBound(test)
MsgBox test(i)
Next i
End Sub