+ Reply to Thread
Results 1 to 9 of 9

How to count the number of cells before a condition is met?

  1. #1
    Registered User
    Join Date
    03-25-2016
    Location
    belgium
    MS-Off Ver
    Office 2010
    Posts
    4

    How to count the number of cells before a condition is met?

    Hi guys,

    I have a nice little problem that I cannot seem to get my head around. Say I have sales data of a product that doesn't sell every day. For instance, in Column A of the attachment, you will find some rows with a positive number but many rows with zeros. In the attachment, I let each row represent a separate date (with the same time interval per row).

    I would like to add a column describing the number of rows it takes to get to the previous sales day with non-zero sales. But I have no clue which formula to use for it. I could use a progressive series of IF statements but this is ugly.
    I have hardcoded the desired result in column B, labeled "intervals".

    Any help is much appreciated!
    Thanks,
    Stuart
    Attached Files Attached Files
    Last edited by svb08; 03-25-2016 at 02:05 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: How to count the number of cells before a condition is met?

    You indicated that you have dates attached to these?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    03-25-2016
    Location
    belgium
    MS-Off Ver
    Office 2010
    Posts
    4

    Re: How to count the number of cells before a condition is met?

    Sorry FDibbins, I meant that one can consider each row to be a date. I have added this to the original post.
    Last edited by svb08; 03-25-2016 at 02:03 PM.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: How to count the number of cells before a condition is met?

    If that is teh case, try this...
    A
    B
    C
    D
    1
    Sales intervals
    2
    1/1/2016
    0
    3
    1/1/2016
    1
    2
    2
    4
    1/2/2016
    0
    5
    1/2/2016
    11
    2
    2
    6
    1/3/2016
    0
    7
    1/3/2016
    0
    8
    1/3/2016
    0
    9
    1/3/2016
    0
    10
    1/3/2016
    2
    5
    5
    11
    1/4/2016
    0
    12
    1/4/2016
    6
    2
    2
    13
    1/5/2016
    3
    1
    1
    14
    1/6/2016
    0

    (I used column D for comparison)...
    D2=IF(B2=0,"",COUNTIFS($A$2:A2,A2))
    copied down

  5. #5
    Registered User
    Join Date
    03-25-2016
    Location
    belgium
    MS-Off Ver
    Office 2010
    Posts
    4

    Re: How to count the number of cells before a condition is met?

    Thanks FGibbins, that would work perfectly if the structure of the data is as you assumed!

    However, what I meant was one different date for each row... I have updated the example in a new attachment. This makes it much harder, at least in my mind...
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,569

    Re: How to count the number of cells before a condition is met?

    This solution makes use of two helper columns*, D and E, with the respective formulas:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    The formula in column C is:
    Please Login or Register  to view this content.
    *Helper columns can be hidden for aesthetic purposes.
    Here is a copy of your file with the helper columns and formulas applied: Copy of example IFERROR(LARGE(INDEX(ADDRESS....xlsx
    Let me know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  7. #7
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2016
    Posts
    5,913

    Re: How to count the number of cells before a condition is met?

    You can use this formula in C2:
    =IF(B2=0,"",ROWS($A$2:A2)-SUM($C$1:C1))
    copied down

  8. #8
    Registered User
    Join Date
    03-25-2016
    Location
    belgium
    MS-Off Ver
    Office 2010
    Posts
    4

    Re: How to count the number of cells before a condition is met?

    Thank you so much guys, for all your clever solutions. Phuocam, can't believe it you did it in one formula :-)
    Thanks again,
    Stuart

  9. #9
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2016
    Posts
    5,913

    Re: How to count the number of cells before a condition is met?

    You're welcome!

+ 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. [SOLVED] Count number of days according to a condition
    By tsakta13ole in forum Excel General
    Replies: 5
    Last Post: 10-06-2015, 08:41 AM
  2. count the number in if condition is met
    By chriskhan90 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-30-2015, 06:35 AM
  3. Replies: 2
    Last Post: 05-20-2015, 08:26 AM
  4. [SOLVED] Formula to count number of blank cells for a specific condition in another column
    By Daniel_12 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-30-2013, 07:16 AM
  5. [SOLVED] count number of cells which meet a particular condition for an individual
    By rohit43 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-18-2013, 11:20 AM
  6. count the number of cells if condition satisfy
    By rosh@excel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-22-2012, 10:09 AM
  7. [SOLVED] how 2 Count number of cells that have specific condition format?
    By daveydavey in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-04-2005, 10:06 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