+ Reply to Thread
Results 1 to 19 of 19

Count odd/even numbers in range with difference of X

  1. #1
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Count odd/even numbers in range with difference of X

    Hello,

    I would like to count how many odd or even numbers in a range have a difference of i.e. 4.

    For example,

    in range 20,24,37,43,45,47,51,89 (A1:H1),

    there is 1 pair of even numbers with a difference of 4 ( numbers 20 & 24) and 2 pairs of Odd numbers with a difference of 4 (43 & 47) & (47 & 51). So the result would be 1 for Even and 2 for Odd

    I am trying to use a sumproduct formula

    =SUMPRODUCT(--((OR(AND(MOD(A1:H1,2)=1,B1-A1=4)...

    but the formula is not very adaptable. Is there a better way of achieving the result?

    Thank you for looking.

  2. #2
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Count odd/even numbers in range with difference of X

    and what about 45 & 47 and 43 & 45?
    If you think that my answer was helpful, please click on the "Add to this user's Reputation" button.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Count odd/even numbers in range with difference of X

    @ Bishonen,

    Hello Bishonen,

    Although the numbers 45 & 47 and 43 & 45 are odd numbers, the difference between them is less than 4,
    and sans only want to count odd and even numbers with a difference of 4.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Count odd/even numbers in range with difference of X

    lol - I'm blind.


    thanks for the clarification.

    Please Login or Register  to view this content.
    Last edited by Bishonen; 10-13-2012 at 06:38 PM.

  5. #5
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Count odd/even numbers in range with difference of X

    @ Bishonen,

    Hello Bishonen,

    No problem, you are welcome.

    And here I am trying to concoct some formula.LOL.

    But WOW! what you came up is fantastic! Well done!

  6. #6
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Count odd/even numbers in range with difference of X

    Dear Winon,

    you're too flattering ^^ I'm pretty sure that the guru's around here could come up with a twice as efficient code twice as fast as I did. Thank you for the reputation as well. Let's just hope that OP will be satisfied with the code. And in the meantime :

    Please Login or Register  to view this content.
    Last edited by Cutter; 10-14-2012 at 11:37 AM. Reason: Removed whole post quote

  7. #7
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Count odd/even numbers in range with difference of X

    Hi guys,

    Thank you very much both for your helpful responses! Bishonen, did you come up with the macro within 3 minutes after Winon's post? That's simply amazing!

    I have 2 columns, one for the Odd results and one for Even results. I imagine that this would require to run 2 macros, one for the Odd Column and one for the Even column. I have tried to modify the macro to get the result for range A1:H1, (going down a few hundreds of rows) but haven't been able to modify it with success. I really do apologise for not making my original post clearer. I am uploading an example where you can see what I mean.

    Thank you for your help!
    Attached Files Attached Files

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Count odd/even numbers in range with difference of X

    Hello sans,

    Thank you for clarifying things for us by supplying us with a sample WorkBook.

    The attached WorkBook is the best solution that I can come up with. In Cell M1 You my enter any value as a difference, and the formulas will respond accordingly. To the right of your data I had to make use of helper columns which you may hide.

    Hope that helps.
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Count odd/even numbers in range with difference of X

    or you can add a couple lines to the last code:

    Please Login or Register  to view this content.
    Perhaps I'm mistaken once again, but it seems to me that your formula, Winon, compares only the adjacent cells. You would have to add 56 of such comparisons for it to work if I'm not mistaken.
    Last edited by Cutter; 10-16-2012 at 11:17 AM. Reason: Removed whole post quote

  10. #10
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Count odd/even numbers in range with difference of X

    @ Bishonen,

    Hello Bishonen,

    I tried your Code and the results don't look right. I am not sure if sans is actually comparing Pairs of Columns, for instance Column A & B, C & D, E and F, etc. Or should any two adjacent columns be compared?

  11. #11
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Count odd/even numbers in range with difference of X

    @Winon,

    Hi Winon!

    As I'm not 100% sure either, I already mentioned that it might be that it's me who's wrong once again. The reason why I thought that we're looking for any match, not only adjacent cells is Sans' first post:

    in range 20,24,37,[b]43,45,47[/b,51],89 (A1:H1),

    there is 1 pair of even numbers with a difference of 4 ( numbers 20 & 24) and 2 pairs of Odd numbers with a difference of 4 (43 & 47) & (47 & 51). So the result would be 1 for Even and 2 for Odd
    Sans states that 43 & 47 should be considered as a pair. Those numbers are separated by 45 however. By your logic, those shouldn't count but yet the end-result in OP's post is 2 for odd.

    But as it's almost 2:00am over here, it might be that my mind is already asleep and I'm terribly wrong:D I'm going to take a nap and check out this thread tomorrow.

  12. #12
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Count odd/even numbers in range with difference of X

    @ Bishonen,

    Hello Bishonen,

    We seem to be on the same time Zone. Also About 2:00 am over here as well. We will take this further tomorrow after a god nap. G'night.

  13. #13
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Count odd/even numbers in range with difference of X

    Hello sans,

    I could not "switch off" and go to sleep. I think I finally got it right.

    I am pretty sure the attached WorkBook returns the answer you are looking for. If you would prefer it in VBA, I'm sure Bishonen could sort that out for you.
    Attached Files Attached Files
    Last edited by Winon; 10-14-2012 at 09:33 PM.

  14. #14
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Count odd/even numbers in range with difference of X

    Hi guys,

    Thank you very much both for your helpful responses! To clarify my original post, a difference of 4 can be between any 2 numbers in the range, not just between the neighboring or adjacent numbers.

    However, I was also trying to come up with an additional formula that would allow me to get the result by comparing 'only' adjacent numbers. So everything worked out great in the end! I can use Bishonen's second macro for dealing with the query in my original post but also found a solution to a formula I was trying to come up with when comparing only adjacent numbers for a difference of 4. So you've helped me with a solution for two issues I was having within one thread!

    Thank you both again for all your help and time. Could not have asked for anything better.

  15. #15
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Count odd/even numbers in range with difference of X

    Hi sans,

    You are welcome.

    Glad we could help in some way.

    but also found a solution to a formula I was trying to come up with when comparing only adjacent numbers for a difference of 4.
    Mind sharing that solution to a formula with us?

  16. #16
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Count odd/even numbers in range with difference of X

    Hi Winon,

    The solution I am referring to is your formula. Sorry for the confusion What I meant above was that both solutions were very helpful.

    Many thanks again

  17. #17
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Count odd/even numbers in range with difference of X

    Hi guys again,

    I was wondering if the above macro can be turned into a UDF as I would like to receive the result for odd/even numbers with a difference of 4,5,6,7,8,9,10 etc. Is it possible to easily "convert" the macro into a UDF as this will save me the time of having to create/run multiple macros upon adding new data?

    Many thanks
    Last edited by sans; 10-20-2012 at 12:15 PM.

  18. #18
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Count odd/even numbers in range with difference of X

    Here's the function for odds. As proof that you're not a lazy bum, its You who's gonna post the code for evens :-)

    Please Login or Register  to view this content.

  19. #19
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Count odd/even numbers in range with difference of X

    No UDF is needed, Native Excel functions will suffice, be faster and use fewer resources.

    The CSE formula
    =SUM((A1:H1-TRANSPOSE(A1:H1)=4)*(MOD(A1:H1,2)=0))
    will return the value for even, while
    =SUM((A1:H1-TRANSPOSE(A1:H1)=4)*(MOD(A1:H1,2)=1)) works for odd.

    The 4 can be altered or replaced with a cell reference to suit your needs.

    Both these formulas need to be entered with Ctrl-Shift-Enter (Cmd+Return for Mac)
    Last edited by mikerickson; 10-20-2012 at 04:50 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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