Countif only one per row

janarthenan

New Member
Joined
Jun 11, 2016
Messages
22
Hi All

I have been trying to achieve the following for days now and can't seem to figure out a way.

I have a spreadsheet with dates when a supervision is done for a particular staff.

I am trying to have a weekly report so that I know on that particular week what my supervision Percentage was.

Every staff needs their supervision done at least every 3 months.

When I do the COUNTIF and if some one had their supervision twice (one in April and one in June) on the June calculation it counts it as 2.

NameAprilMayJuneJulyAugustSeptOctNovDecLast SupervisionNext Supervision
A1/4/171/6/171/7/17=MAX(B2:J2)=H2+90
B1/5/171/8/17=MAX(B3:J3)=H3+90
C....
D
E
F

<tbody>
</tbody>

When I do COUNTIF(B2:J2, "<="&1/4/17) - I get 1
When I do COUNTIF(B2:J2, "<="&1/7/17) - I get 3 (Which is correct) but I want this to be one.
- So basically I want the calculation to count the highest value only per row and If that highest value meets the COUNTIF condition to come back as 1.

Prefer if no VBA was used.

Regards
J
 
Hi Peter

Your creation has not failed me, but i was wondering if you can help me with one last thing.

I would like the formula to Omit a row if "0" is present in that column.

Is this possible?

Jan
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I would like the formula to Omit a row if "0" is present in that column.
What column?

This is not fresh in my mind, so can you post another small set of sample data, expected result(s) and explanation of this new requirement in relation to that sample data?
 
Upvote 0
Hi Peter

This was the code you originally created for me:

Code:
Function Count_Rows_With_Ignore(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("d", -90, DateToCheck)
  'EarlierDate = DateAdd("m", -3, DateToCheck)
  For i = 1 To UBound(aRangeToLookIn, 1)
    If IsEmpty(aIgnore(i, 1)) Then
      For j = 1 To UBound(aRangeToLookIn, 2)
        If aRangeToLookIn(i, j) <= DateToCheck Then
          If aRangeToLookIn(i, j) >= EarlierDate Then
            Count_Rows_With_Ignore = Count_Rows_With_Ignore + 1
            Exit For
          End If
        End If
      Next j
    End If
  Next i
End Function

It looks at a range of data and counts the number of rows that have a date within the last 90 days (3 months).
If there are two dates in a row, it counts the newest date.
It also ignores a row, if a value is entered in a "Leaver" Column.

Now I would like to add another column "Years of Service" and if the value is 0 here to ignore these rows too.

BCDEFGHI
2105/06/2017 0HSCW06/06/2017
3203/10/2016 1HSCW
4304/08/2017 0HSCW
5402/05/2017 0HSCW
6501/08/2014 3SHSC01/04/201726/07/2017
7606/11/2017 0HSCW
8714/08/2017 0HSCW
9823/10/2017 0HSCW
10921/02/2017 1COOK07/04/2017
111014/11/2016 1HSCW05/05/2017

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Appraisals
Column D is the years of service and Row 1 contains a date in H, the count should ignore this as column D contains a 0.

To explain - These are people who are due appraisals, sometimes the appraisals are completed way before they are due and I want these to be ignored.

Hope this explains it.

Thank You Once again

Jan
 
Upvote 0
OK, try this
Code:
Function Count_Rows_With_2Ignores(RngToLookIn As Range, DateToCheck As Date, rngIgnoreLeavers As Range, rngIgnoreYears As Range) As Long
  Dim aRangeToLookIn As Variant, aIgnoreLeaver As Variant, aIgnoreYears As Variant
  Dim EarlierDate As Date
  Dim i As Long, j As Long
  
  aRangeToLookIn = RngToLookIn.Value
  aIgnoreLeaver = Intersect(RngToLookIn.EntireRow, rngIgnoreLeavers.EntireColumn).Value
  aIgnoreYears = Intersect(RngToLookIn.EntireRow, rngIgnoreYears.EntireColumn).Value
  EarlierDate = DateAdd("d", -90, DateToCheck)
  For i = 1 To UBound(aRangeToLookIn, 1)
    If Len(aIgnoreLeaver(i, 1)) = 0 And aIgnoreYears(i, 1) <> 0 Then
      For j = 1 To UBound(aRangeToLookIn, 2)
        If aRangeToLookIn(i, j) <= DateToCheck Then
          If aRangeToLookIn(i, j) >= EarlierDate Then
            Count_Rows_With_2Ignores = Count_Rows_With_2Ignores + 1
            Exit For
          End If
        End If
      Next j
    End If
  Next i
End Function

Used like this for example
=Count_Rows_With_2Ignores(F$2:N$11,C15,Q$2:Q$11,D$2:D$11)

Red range is the main range where the supervision dates are recorded
Blue cell is the date that you are checking against
Green range is the column containing any 'Leaver' information
Pink range is the column containing the years of service
 
Upvote 0
Try the UDF below. For the layout in post 10 with the additional 'Leaver' column in column Q, use it like this

=Count_Rows_With_Ignore(B$3:N$10,D14,$Q$3:$Q$10)

Code:
Function Count_Rows_With_Ignore(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("d", -90, DateToCheck)
  'EarlierDate = DateAdd("m", -3, DateToCheck)
  For i = 1 To UBound(aRangeToLookIn, 1)
    If IsEmpty(aIgnore(i, 1)) Then
      For j = 1 To UBound(aRangeToLookIn, 2)
        If aRangeToLookIn(i, j) <= DateToCheck Then
          If aRangeToLookIn(i, j) >= EarlierDate Then
            Count_Rows_With_Ignore = Count_Rows_With_Ignore + 1
            Exit For
          End If
        End If
      Next j
    End If
  Next i
End Function

Hi

Is there a way in the RngToLookIn to select two columns that are not next to each other?

Thank You for your help

J
 
Upvote 0
Hi

Is there a way in the RngToLookIn to select two columns that are not next to each other?

Thank You for your help

J
Could we have some sample data in the layout you are now asking about, and the expected results and explain again in relation to that sample data?
 
Upvote 0
Hi

I am using the following code currently, what i require is, the RngToLookIn are not adjacent, they are several columns apart.
Is there a way to select the range when they are several columns away?

Please see example below code.

Code:
Function Count_Rows_Supervision(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("d", -90, DateToCheck)
  'EarlierDate = DateAdd("m", -3, 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_Rows_Supervision = Count_Rows_Supervision + 1
            Exit For
          End If
        End If
      Next j
    End If
  Next i
End Function

ABCDEFGHIJK
1STATUTORY AND MANDATORY TRAININGStart DateDesignationInfection controlMoving People Safely - PracticalSafeguarding AdultsLeaverInfection control - OLDMoving People Safely - Practical - OLDSafeguarding Adults - OLD
2Staff A16/05/2018SN21/08/1821/08/1821/08/1818/05/17
3Staff B27/03/2018HSCW09/10/1810/10/1815/08/1822/01/1701/01/15
4Staff C03/10/2016HSCW15/08/1801/07/1815/08/1810/01/17
5Staff D06/11/2017HSCW15/08/1811/12/1706/11/171/11/1711/12/1610/03/16
6Staff E08/04/2014Domestic
7Staff F05/03/2018HSCW15/08/1813/03/1815/08/1812/01/1715/04/17
8Staff G23/10/2017HSCW31/10/1811/12/1731/10/1810/09/16
9Staff H21/02/2017COOK12/08/1712/08/1712/06/1615/09/17
10Staff I05/03/2018HSCW15/08/1817/07/1815/08/18

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1




The RangeToLookIn is Column D, with Column I
DateToCheck is Todays Date
rngIgnore is Column G

I am able to select multiple columns when they are next to each other, but when they are several columns apart it doesnt work.

Please advice
J
 
Upvote 0
I'm sorry, I haven't been able to spend time on the forum for a while and that will continue for a while longer.
 
Upvote 0
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

<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])

<tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,705
Members
449,048
Latest member
81jamesacct

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