+ Reply to Thread
Results 1 to 9 of 9

Count number of cells in a range where values differ by 1, 2 or 3

  1. #1
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    MS-Off Ver
    Using Excel 2019
    Posts
    317

    Count number of cells in a range where values differ by 1, 2 or 3

    Hi,

    I've got two ranges (A1:E1) and (A2:E2) which contain single digit numbers.

    I have a formula to count how many values are equal to each other in both ranges

    =SUMPRODUCT(--(A1:E1=A2:E2))
    I'd now like to find out how many times the values in A2:E2 are less than/more than A1:E1 by 1,2 or 3.

    Can anyone help?

    Thanks
    Last edited by Barking_Mad; 09-12-2018 at 06:13 AM.

  2. #2
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    For a difference of 1

    =SUMPRODUCT(--(ABS(A1:E1-A2:E2)=1))

    Change the last digit for other differences.

    Or to get the total of all differences in one formula

    =SUMPRODUCT(--(ABS(A1:E1-A2:E2)={1;2;3}))

  3. #3
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    =SUMPRODUCT(--(A1:E1=A2:E2)-1)
    =SUMPRODUCT(--(A1:E1<A2:E2)-1)


    etc you get the idea or the previous post if you dont care about the direction of the difference
    Last edited by davsth; 09-12-2018 at 06:11 AM.

  4. #4
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    MS-Off Ver
    Using Excel 2019
    Posts
    317

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    Cheers guys. I was on the right track, but as usual by brackets were in the wrong places!

  5. #5
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    Quote Originally Posted by davsth View Post
    =SUMPRODUCT(--(A1:E1=A2:E2)-1)
    =SUMPRODUCT(--(A1:E1<A2:E2)-1)
    Those formulas will not give the results that you think, the brackets are all wrong and will result in flipped polarities.

    First one will give result of =SUMPRODUCT(--(A1:E1<>A2:E2))
    Second on will give result of =SUMPRODUCT(--(A1:E1>A2:E2))

    But both will return the result as negative

  6. #6
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    MS-Off Ver
    Using Excel 2019
    Posts
    317

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    Hmmm maybe Im misunderstanding something or I didnt explain it very well.......

    4 3 5 4 4 4 4 3 5 4 3 4 4 5 4 3 4 4
    4 2 3 4 4 3 5 3 5 3 3 5 3 5 4 3 4 4

    =SUMPRODUCT(--(ABS(A1:R1-A2:R2)=1))

    Should give a result of 4, but actually gives it as 6. Edit: So it's summing the differences, I just want to know how many times a number is below a specific value.

    (if it helps any these are golf scores, with the top row being the par and the row underneath being the score. I'm trying to work out how many scores are birdie (-1), eagle (-2) bogie (+1) double bogie (+2)
    Last edited by Barking_Mad; 09-12-2018 at 06:50 AM.

  7. #7
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    MS-Off Ver
    Using Excel 2019
    Posts
    317

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    I think I sorted it. With the following as an array formula

    {=COUNT(IF((A1:R1-A2:R2)=1,A1:R1))}

  8. #8
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    Jason bad positing you are right, it was not what I had written in the spreadsheet, hangs head in shame!

    the result is that the formula is also looking at the other way

    4 3 5 4 4 4 4 3 5 4 3 4 4 5 4 3 4 4
    4 2 3 4 4 3 5 3 5 3 3 5 3 5 4 3 4 4

    so together they are 6
    =SUMPRODUCT(--(A1:R1=A2:R2-1))
    =SUMPRODUCT(--(A1:R1-1=A2:R2))

    get you to the 6 calculated
    Last edited by davsth; 09-12-2018 at 07:09 AM.

  9. #9
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count number of cells in a range where values differ by 1, 2 or 3

    Quote Originally Posted by Barking_Mad View Post
    =SUMPRODUCT(--(ABS(A1:R1-A2:R2)=1))

    Should give a result of 4, but actually gives it as 6. Edit: So it's summing the differences, I just want to know how many times a number is below a specific value.

    (if it helps any these are golf scores, with the top row being the par and the row underneath being the score. I'm trying to work out how many scores are birdie (-1), eagle (-2) bogie (+1) double bogie (+2)
    To get either / or, simply remove the ABS function from the sumproduct formula

    =SUMPRODUCT(--((A1:R1-A2:R2)=1))

    It is not summing the difference, but counting both 1 under and 1 over, as you asked for more/less than in your original question the formula was tailored for both.

    Bearing in mind the golf scenario, I would reverse the formula to

    =SUMPRODUCT(--((A2:R2-A1:R1)=-1))

    To give the correct count of birdie / 1 under par, then adjust the criteria for other scores.

    If your scoring tally goes from 2 over par to 'hang up your clubs' without counting the individual 3 over, 4 over, etc. then you could use

    =SUMPRODUCT(--((A1:R1-A2:R2)>=3)) for 3 over or more.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Send an email with a dynamic range of cells that differ depending on the date
    By clementvenot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2017, 05:58 AM
  2. [SOLVED] Count number of cells in range since last number greater than zero
    By whitfieldcraig in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-18-2015, 06:38 AM
  3. [SOLVED] Determine whether values in 3 cells differ by more than 20%
    By rndmwrd in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-25-2014, 10:56 PM
  4. Count if: Compare 2 ranges and count where cells in same row differ
    By ExcelFed in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-15-2014, 02:34 PM
  5. Replies: 8
    Last Post: 07-14-2013, 07:29 PM
  6. Replies: 6
    Last Post: 11-11-2012, 12:40 AM
  7. Count the number of values that fall with in a range
    By nynamyna in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2011, 08:39 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1