title: Excel VBA Countif between dates
body: <p>I am needing to create a function to count the occurrences of an account number between two dates. The first date is based on the function input and the second is 3 months in advance (date may not be contained within the data set). Date values in the range are in the format "dd/mm/yyyy h:mm". Due to the size of the dataset approx 150,000 lines i am wanting to perform this in the code and not paste or evaluate the COUNTIF formula within a specified cell.</p>
  
  <p>The worksheet function works when only the AccountNo variable is referenced but not when the conditional ">=" or "&lt;=" Date variables are added</p>
  
  <p>e.g. Application.WorksheetFunction.CountIfs(Range("L2:L" &amp; Endrow), AccountNo) > 1 Then ''''(Works)</p>
  
  <p>The function needs to return a result based on the countif result as below.</p>
  
  <p>Thanks,</p>
  
  <pre><code>Function LastWrapUp(Date1 As Date, AccountNo)
  
  Dim Date2 As Date
  Dim Endrow As Long
  
  Date2 = DateAdd("M", 3, Date1)
  Endrow = Range("A" &amp; Rows.Count).End(xlUp).Row
  
  If Application.WorksheetFunction.CountIfs(Range("A2:A17643"), "&gt;=" &amp; Date1, Range("A2:A" &amp; Endrow), "&lt;" &amp; Date2, Range("L2:L" &amp; Endrow), AccountNo) &gt; 1 Then
      LastWrapUp = "Not Final Wrap Up"
  ElseIf Application.WorksheetFunction.CountIfs(Range("A2:A" &amp; Endrow), "&gt;=" &amp; Date1, Range("A2:A" &amp; Endrow), "&lt;" &amp; Date2, Range("L2:L" &amp; Endrow), AccountNo) = 1 Then
      LastWrapUp = "Yes"
  Else
      LastWrapUp = "Error"
  End If
  
  Debug.Print LastWrapUp
  
  End Function
  </code></pre>