Count - VBA Help

janarthenan

New Member
Joined
Jun 11, 2016
Messages
22
Hi All

I currently have the below UDF for my spreadsheet.
It looks at two columns and counts the highest date, if the leavers column is empty and the date entered is less than the DateToCheck

I would like to add 2 more variables to this UDF if possible.

1. I would the UDF to count based on Designation which is on a seperate level table.
2. I would like the UDF to ignore row if the StartDate is after DateToCheck.

Also currently the Previous and the Current columns are next to each other, THe UDF does not work if the columns are not next to each other.
I would like to keep the Previous columns together.

Code:
Function Count_Training(RngToLookIn As Range, DateToCheck As Date, rngIgnore As Range) As Long  Dim aRangeToLookIn As Variant, aIgnore As Variant
  Dim EarlierDate As Date
  Dim i As Long, j As Long
  
  aRangeToLookIn = RngToLookIn.Value
  aIgnore = Intersect(RngToLookIn.EntireRow, rngIgnore.EntireColumn).Value
    EarlierDate = DateAdd("m", -12, DateToCheck)
  For i = 1 To UBound(aRangeToLookIn, 1)
    If Len(aIgnore(i, 1)) = 0 Then
      For j = 1 To UBound(aRangeToLookIn, 2)
        If aRangeToLookIn(i, j) <= DateToCheck Then
          If aRangeToLookIn(i, j) >= EarlierDate Then
            Count_Training = Count_Training + 1
            Exit For
          End If
        End If
      Next j
    End If
  Next i
End Function

ABCDEFGHIJ
1TraineeStart DateDesignationInfection controlInfection control - PreviousSafeguarding AdultsSafeguarding Adults - PreviousBasic Life SupportBasic Life Support - PreviousLeaver
2A03/01/2003HSCWBank
3B01/03/2003MANAGER20/12/1820/12/1815/12/1820/12/1823/12/18
4C01/09/2003DOMESTIC01/10/1802/04/1502/04/15
5D01/08/2005SN01/07/1807/01/1707/01/1720/05/1707/01/17Leaver
6E30/04/2006SN29/12/1829/12/1829/12/1821/10/1821/10/18
7F04/06/2007SHSC16/08/1816/08/1816/08/1821/05/1716/08/1811/10/18
8G06/11/2008SN30/07/1830/07/1830/07/1830/07/18
9H01/02/2009SN15/07201815/07201815/07/1819/09/1720/04/17
10I13/02/2010SHSC03/07/1703/07/1723/05/1707/07/1701/11/16Leaver
11J20/06/2010SN25/05/1825/05/1825/05/1825/05/1825/05/18
12K28/06/2010SHSC15/08/1815/08/1815/08/1804/11/1703/12/17
13L07/10/2010D.MANAGER31/10/1831/10/1831/10/1831/10/1831/10/1812/10/18
14M26/10/2010SHSC15/08/1815/08/1815/08/1815/08/1823/11/16
15N09/05/2011SHSC14/12/1814/12/1831/10/1818/10/1809/12/1611/10/18
16O31/10/2011ADMIN08/09/1508/09/1508/09/15Leaver
17
18
19Infection ControlSafeguarding AdultsBasic Life SupportLevel 1Level 2Level 3
20Training Stats10107HSCWDOMESTICMANAGER
21SNADMIND. MANAGER
22SHSCW

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Stat. & Mand. Training

Worksheet Formulas
CellFormula
D20=Count_Training(Tier1[[Infection control]:[Infection control - Previous]],TODAY(),Tier1[Leaver])
E20=Count_Training(Tier1[[Safeguarding Adults]:[Safeguarding Adults - Previous]],TODAY(),Tier1[Leaver])
F20=Count_Training(Tier1[[Basic Life Support]:[Basic Life Support - Previous]],TODAY(),Tier1[Leaver])

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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