+ Reply to Thread
Results 1 to 16 of 16

Count number of cells between first and last non-zero value in row

  1. #1
    Registered User
    Join Date
    11-25-2018
    Location
    Bangalore, India
    MS-Off Ver
    365
    Posts
    2

    Question Count number of cells between first and last non-zero value in row

    Hi
    I want to count the number of cells between the first and last non-zero value in a row.
    Ex: Below row of numbers should return 6 (6 cells from the first non-zero value i.e. 4 to the last one i.e. 3).

    0 0 4 5 0 0 2 3 0 0 0

    Can someone help with the right function for this?

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Count number of cells between first and last non-zero value in row

    Welcome to the forum!

    Try this:

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    1
    0
    0
    4
    5
    0
    0
    2
    3
    0
    0
    0
    2
    3
    6
    Sheet: Sheet1

    Excel 2016 (Windows) 32 bit
    B
    3
    =(LOOKUP(2,1/(1:1>0),COLUMN(1:1))+1)-(MATCH(1,1:1,1)+1)
    Sheet: Sheet1
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    11-25-2018
    Location
    Bangalore, India
    MS-Off Ver
    365
    Posts
    2

    Re: Count number of cells between first and last non-zero value in row

    Thanks! Could you pls explain how that worked?

  4. #4
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    Hi,

    =MMULT(AGGREGATE({14,15},6,COLUMN(A1:K1)/A1:K1^0,1),{1;-1})+1

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  5. #5
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    Quote Originally Posted by AliGW View Post
    Welcome to the forum!

    Try this:

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    1
    0
    0
    4
    5
    0
    0
    2
    3
    0
    0
    0
    2
    3
    6
    Sheet: Sheet1

    Excel 2016 (Windows) 32 bit
    B
    3
    =(LOOKUP(2,1/(1:1>0),COLUMN(1:1))+1)-(MATCH(1,1:1,1)+1)
    Sheet: Sheet1
    What happens if, for example, the first and last entries are non-zero and all the rest zero?

    Regards

  6. #6
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    Hi Ali,

    Just noticed why your formula won't always give correct results. With a match_type parameter of 1, the lookup_array must be in ascending order.

    Regards

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Count number of cells between first and last non-zero value in row

    It won't matter in this case (I did think about it!) - we are looking for the first number larger than or equal to 1.

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    1
    0
    0
    22
    3
    0
    0
    2
    3
    0
    0
    0
    2
    3
    6
    Sheet: Sheet1

    As for your query in post #5: I can't imagine a scenario like that given the information given. Anyway, the OP seems happy with it.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Count number of cells between first and last non-zero value in row

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  9. #9
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    Quote Originally Posted by AliGW View Post
    It won't matter in this case (I did think about it!)
    Sorry, what won't matter? You mean the fact that the values in the range aren't in ascending order won't affect your choice of MATCH with a match_type parameter of 1?

    I hope not, since that is an absolute requisite for that function with that parameter!

    Regards

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Count number of cells between first and last non-zero value in row

    What I mean is that it does not matter what the first number larger than 0 is or whether it is in ascending order in the range that follows - it's only that first number that matters in this case and the formula returns the correct column regardless of what that number is. It can be bigger than, the same as or smaller than the second number bigger than 0 - the formula will still work. In other words, the MATCH is only going to return the column number of the first match. Perhaps you could show me in what way you think it fails?

  11. #11
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    0
    2
    1
    9
    7
    1
    2
    0
    3
    2
    0

    The first and last non-zeroes in the above are highlighted. The result should be 9, yet your formula returns 2.

    Regards

  12. #12
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    Quote Originally Posted by XOR LX View Post
    Hi,

    =MMULT(AGGREGATE({14,15},6,COLUMN(A1:K1)/A1:K1^0,1),{1;-1})+1

    Regards
    Apologies. That should've been:

    =1+MMULT(AGGREGATE({14,15},6,COLUMN(A1:K1)/A1:K1^0,1),{1;-1})

    Regards

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Count number of cells between first and last non-zero value in row

    Ah, I see what you mean. Back to the drawing board!

  14. #14
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    Actually we can combine two MATCH statements as well, so this works for pre-Excel 2010:

    =1+MMULT(MATCH({1,0},0/1:1,{1,0}),{1;-1})

    Regards

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,719

    Re: Count number of cells between first and last non-zero value in row

    That's a nice compromise!

  16. #16
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Count number of cells between first and last non-zero value in row

    Quote Originally Posted by AliGW View Post
    That's a nice compromise!
    Compromises are good!

+ 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. how to count number of blank cells on a column up to a number value is typed
    By carlosnixon in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-25-2018, 02:46 AM
  2. how to count number of blank cells on a column up to a number value is typed
    By carlosnixon in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-25-2018, 01:01 AM
  3. [SOLVED] How to count *backwards* the number of blank cells until a number reached
    By lamlam28 in forum Excel Formulas & Functions
    Replies: 19
    Last Post: 05-06-2018, 11:34 PM
  4. [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
  5. count number of cells since value exceeded some number
    By abhijith_ca in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-25-2013, 12:58 AM
  6. Replies: 2
    Last Post: 08-16-2010, 10:44 AM
  7. Count number of times a specific number is displayed in cells
    By subs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2005, 11:05 AM

Tags for this Thread

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