I'm trying to use a loop variable as autofilter criteria. The loop is working fine when I put the autofilter criteria manually. When I try to integrate my variable (i), it is not working anymore.
Column A contains a series of material numbers ranging from 1-115. One by one I need to filter them and calculate the MIN, MAX, AVERAGE,... on column C values. The range for the formulas is the number of rows filtered. This part is working fine.
As I don't want to repeat this code 115 times, I want to use a loop. The loop itself is working fine.
The autofilter should use the value for I (loop counter) in the selection criteria.
This is not working.
Dim rijen As Integer
Dim start As Integer
Dim filterA As String
Dim filterB As String
Dim aantal As Integer
Dim i As Integer
range("A2").Select
Selection.End(xlDown).Select
aantal = ActiveCell.Value
i = 1
Do Until i > aantal
range("A2").Select
' THIS IS THE PART THAT IS NOT WORKING
ActiveSheet.range("$A$2:$H$1045876").AutoFilter Field:=1, Criteria1:=" & i & "
'THIS IS WORKING FINE AGAIN
ActiveSheet.AutoFilter.range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, A).Select
start = ActiveCell.Row
range("A2").Select
Selection.Offset(1, 0).Select
rijen = range(Selection, Selection.End(xlDown)).Rows.Count + start - 1
filterA = ("C" & start)
filterB = ("C" & rijen)
range("D2").Select
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=MIN(" & filterA & ":" & filterB & ")"
range("E2").Select
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=MAX(" & filterA & ":" & filterB & ")"
range("F2").Select
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=GEMIDDELDE(" & filterA & ":" & filterB & ")"
range("G2").Select
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=STDEVA(" & filterA & ":" & filterB & ")"
range("H2").Select
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=MIN(RC[-3],(RC[-2]+RC[-1]))"
ActiveSheet.range("$A$2:$H$1045876").AutoFilter Field:=1
i = i + 1