Countifs code with a date range

571202

New Member
Joined
May 26, 2019
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi All, I would like to run the below code four times with four different date ranges, 1. This month, 2. Last month, 3. Last quarter, 4. Previous quarter (the quarter prior to last quarter). I would like them NOT static, but rolling forward with time if possible, cheers.

Code:
Private Sub Count_KPI11()With Sheets("Last Mth Processed")
.Range("E5").Value = CountA("Work", "WSCA1", "Processed")
.Range("E6").Value = CountA("Work", "WSCA2", "Processed")
.Range("E7").Value = CountA("Work", "SECA11", "Processed")
.Range("E8").Value = CountA("Work", "SECA12", "Processed")
.Range("E9").Value = CountA("Work", "SECA13", "Processed")
.Range("E17").Value = CountA("Work", "NWCA5", "Processed")
.Range("E18").Value = CountA("Work", "NWCA6A", "Processed")
.Range("E19").Value = CountA("Work", "NWCA6B", "Processed")
.Range("E20").Value = CountA("Work", "NWCA7", "Processed")
.Range("E21").Value = CountA("Work", "NWCA8", "Processed")
.Range("E22").Value = CountA("Work", "NWCA9", "Processed")
.Range("E23").Value = CountA("Work", "NWCA10A", "Processed")
.Range("E24").Value = CountA("Work", "NWCA10B", "Processed")
.Range("E32").Value = CountA("Work", "WSCA3", "Processed")
.Range("E33").Value = CountA("Work", "WSCA4", "Processed")
.Range("E34").Value = CountA("Work", "SECA14", "Processed")
.Range("E35").Value = CountA("Work", "SECA15", "Processed")
.Range("E36").Value = CountA("Work", "SECA16", "Processed")
End With
End Sub
Function CountA(v1 As String, v2 As String, Optional v3 As String = "") As Long
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    With Sheets("calculations")
    
        Set rng1 = .Range("D7:D100000")
        Set rng2 = .Range("L7:L100000")
        Set rng3 = .Range("M7:M100000")
        If Len(v3) > 0 Then
            CountA = Application.CountIfs(rng1, v1, rng2, v2, rng3, v3)
        Else
            CountA = Application.CountIfs(rng1, v1, rng2, v2)
        End If
    End With
End Function
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi All, I would like to run the below code four times with four different date ranges, 1. This month, 2. Last month, 3. Last quarter, 4. Previous quarter (the quarter prior to last quarter). I would like them NOT static, but rolling forward with time if possible, cheers.

Code:
Private Sub Count_KPI11()With Sheets("Last Mth Processed")
.Range("E5").Value = CountA("Work", "WSCA1", "Processed")
.Range("E6").Value = CountA("Work", "WSCA2", "Processed")
.Range("E7").Value = CountA("Work", "SECA11", "Processed")
.Range("E8").Value = CountA("Work", "SECA12", "Processed")
.Range("E9").Value = CountA("Work", "SECA13", "Processed")
.Range("E17").Value = CountA("Work", "NWCA5", "Processed")
.Range("E18").Value = CountA("Work", "NWCA6A", "Processed")
.Range("E19").Value = CountA("Work", "NWCA6B", "Processed")
.Range("E20").Value = CountA("Work", "NWCA7", "Processed")
.Range("E21").Value = CountA("Work", "NWCA8", "Processed")
.Range("E22").Value = CountA("Work", "NWCA9", "Processed")
.Range("E23").Value = CountA("Work", "NWCA10A", "Processed")
.Range("E24").Value = CountA("Work", "NWCA10B", "Processed")
.Range("E32").Value = CountA("Work", "WSCA3", "Processed")
.Range("E33").Value = CountA("Work", "WSCA4", "Processed")
.Range("E34").Value = CountA("Work", "SECA14", "Processed")
.Range("E35").Value = CountA("Work", "SECA15", "Processed")
.Range("E36").Value = CountA("Work", "SECA16", "Processed")
End With
End Sub
Function CountA(v1 As String, v2 As String, Optional v3 As String = "") As Long
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    With Sheets("calculations")
    
        Set rng1 = .Range("D7:D100000")
        Set rng2 = .Range("L7:L100000")
        Set rng3 = .Range("M7:M100000")
        If Len(v3) > 0 Then
            CountA = Application.CountIfs(rng1, v1, rng2, v2, rng3, v3)
        Else
            CountA = Application.CountIfs(rng1, v1, rng2, v2)
        End If
    End With
End Function
I should have mentioned the date column is dd/mm/yyyy format, cheers.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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