+ Reply to Thread
Results 1 to 21 of 21

Countif

  1. #1
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Countif

    Hi,

    I’m attempting to write a macro that calculates how many passengers got on a train. So if the passenger time is less than the train time add 1 to count.

    As you can see I’ve highlighted what I mean, so for the 0551 train there were 4 passengers.

    I’ve started but to be honest I’m a bit lost. I assume I’d need a loop to check that passenger time is less than train time, if it is then add 1 to count, if it’s not place the count in the totals cell and carry on the check for the next train at 0621 but ignoring those passengers already used for the 0551 train.

    Any help much appreciated.

    Cheers.
    Attached Files Attached Files

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    Do you have a particular need for a macro rather than a formula?
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    Hi romper,

    a forumla is fine, i just presumed that what i was asking for would require a macro.

    i may also have to add that this will be used on other stations where the train times will be different along with the passenger times.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    Is there some significance to the order of the data in the left hand table? (it doesn't appear to be sorted just by train time)

  5. #5
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    there is yes, the WI is inbound the WO is outbound, only in this instance there is no outbound data. so the passenger data only applies to all that is WI.

    for other stations i would have to take the VAR in to account, but hopefully i can figure that once this bit is clearer.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    In that case, in F2 enter:
    =SUMPRODUCT(--($M$2:$M$234<=C2))-F1
    and copy down to the end of the WI data.

  7. #7
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    Cheers romper, this works for the first two, but then in cell F4 the answer returned is 11, it should be 7, so it is subtracting the previous total but not the one before that.

    So when I copy down, F24 only subtracts F23 where it should be subtracting all of them.. if that makes sense.

    I tried to change the formula but to no avail.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    Sorry - brain fart. It should be:
    =SUMPRODUCT(--($M$2:$M$234<=C18))-SUM(F$1:F17)
    Note: your times are not sorted in order - they need to be for this to work properly. (you have 1006 after 1021 in your data)

  9. #9
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    you're a star romper, cheers.

    I might be back on here in a few days when i try this on a more complicated station with more data!

    Thanks again, really appreciate it.

  10. #10
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    ok stuck again, and i know this is simple but no matter what i try i cant get it to work.

    I want to find the total ticket types for each train, so i assumed i'd use the INDIRECT to count cell C2 to C(then whatever number is in E2). i planned on pairing this with a count if across the ticket types.

    sheet attached.
    Attached Files Attached Files

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    It's not clear to me what you are trying to do there. INDIRECT requires a cell reference, and you are only passing it a row number, without a column letter.

  12. #12
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    hey romper,

    i figured the code to add the cells which is

    Please Login or Register  to view this content.
    which adds up and gives me 4, but this isn't great as i need to nest this somehow within a sumif or countif to say within the selected data, count the number of ticket types that equal 1, and so on for the rest of the ticket types.

    not sure if indirect is what it needs. as it gets more complicated to tally them up when you drag it down as it totals everything up from cell C2.

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    So where is it supposed to start from for the next row? C8?

  14. #14
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    it's like what you did for me with the sumproduct really, but with ticket types.

    the first train 0551 is highlighted red on the int data sheet, all with TType of 1, so the summary sheet would show AC1 as 4.

    then the next train at 0621 would show 6,1 & 3.

    am i making sense?

    the ticket type total should equal the No of ints total.

    ignore the formula i've used because the more i mess with it the less likely it is to work.

    is there a better way?

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    How's this? (note: you have one ticket of type 0, hence the discrepancy.)
    Attached Files Attached Files

  16. #16
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Complicated Countif

    Re Post #14

    Is this the same question?
    http://www.excelforum.com/excel-gene...te-totals.html

  17. #17
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    perfect. i knew it must be a combined sumproduct but just couldnt figure it out.

    can you talk me through the formula so i can understand what each part is doing?

  18. #18
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    Thanks - I've locked the other one.

    The formula counts the tickets that match the specified type and have a train time less than that train's time, then subtracts the rows above to get the count for just that train. Very similar to how the other one worked.

  19. #19
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    Hey romper,

    been cracking on with this, and what you've given me is a massive help, never used sumproduct before.

    what I was wondering is can you extend the range to say all of coulumn D? rather than at the moment its looking at cells $D$2:$D$227, when i try it it doesnt work.

  20. #20
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Complicated Countif

    As long as you are using 2007, yes you can, but it won't work in 2003 with an entire column. I'd suggest you either set the range to a number of rows larger than you will use (say 10,000) or use a dynamic named range.

  21. #21
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Complicated Countif

    Hi Romper,

    I used your advice and just increased the range.

    I’m pretty much finished now bar the odd tweak so thanks again. However, it seems to be taking a very long time to calculate cells, which isn’t ideal, I’m wondering if some of the formulas I have used are too cumbersome?

    Can you have a goosies and let me know what you think might be slowing it down so much?

    Cheers
    Attached Files Attached Files

+ 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