VBA code for filtering using 5 variables criteria

chiidzzz

New Member
Joined
Sep 2, 2012
Messages
32
Hi, how can I add 4 more variable criteria to this code, is it possible?
Code:
ActiveSheet.Range(Range("A2"), Range("R2").End(xlDown)).AutoFilter Field:=1, Criteria1:=ThisYearID
criteria2 should be :ThisYearID-1
criteria3 should be :ThisYearID-2
criteria4 should be :ThisYearID-3
criteria5 should be :ThisYearID-4
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Do all those criteria together equate to >ThisYearID-5 ?
 
Upvote 0
Code:
Sub test_calculateval()
Dim rnData, v, r As Long, ThisYearID, Hcount As Long, Hometeam As String
ThisYearID = Sheet5.Cells(2, 1).Value - 1
Hometeam = Sheet5.Cells(2, 5)
With Sheet1
    Set rnData = Range(Range("A2"), Range("R2").End(xlDown))
           With rnData
            Rows("1:1").Select
            Selection.AutoFilter
            ActiveSheet.Range(Range("A2"), Range("R2").End(xlDown)).AutoFilter Field:=1, Criteria1:=">" & ThisYearID - 5
            ActiveSheet.Range(Range("A2"), Range("R2").End(xlDown)).AutoFilter Field:=5, Criteria1:=Hometeam
           End With
           
    Set v = ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).Row
    Set r = ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, Rows.count).Row
    
  For i = v To r
  
    If ActiveSheet.UsedRange.SpecialCells _
        (xlCellTypeVisible).Areas.Columns(11).Cells(i, 11) = "H" Then
            Hcount = Hcount + 1
    End If
    
  Next
  
End With


MsgBox Hcount


End Sub
Any idea how to fix the counting of "H" in column 11?
 
Upvote 0
You should use Option Explicit and declare all your variables.
Have a look at this page, in particular the part about Variables Declared With One Dim Statement.

Seldom is there a need to Select anything in VBA.
Your Row("1:1").Select makes me think row 1 is the data headers.
See if this helps
Code:
Sub test_calculateval()
    Dim rnData As Range, Hcount As Long
    Dim ThisYearID As Long, Hometeam As String
    Dim cel As Range
    
ThisYearID = Sheet5.Cells(2, 1).Value - 1
Hometeam = Sheet5.Cells(2, 5)

With Sheet1
    ' this assumes headers are in row 1
    Set rnData = .Range(.Range("A1"), .Range("R2").End(xlDown))
End With

With rnData
    .AutoFilter Field:=1, Criteria1:=">" & ThisYearID - 5
    .AutoFilter Field:=5, Criteria1:=Hometeam
End With
    
    For Each cel In rnData.Columns(11).SpecialCells(xlCellTypeVisible)
        If cel.Value = "H" Then
            Hcount = Hcount + 1
        End If
    Next cel

    MsgBox Hcount

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top