+ Reply to Thread
Results 1 to 6 of 6

Loop to Sum Unknown Number of Rows

  1. #1
    Registered User
    Join Date
    10-15-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    Loop to Sum Unknown Number of Rows

    Hi.

    I am new to VBA but have other programming experience. I have been asked to create a macro to automate a manual process. This has been going fine until I hit this stumbling block.

    Any help, or a link to an existing solution would be gratefully received.

    Jack 10
    Jill 10
    Peter 2
    Peter 2
    Peter 2
    Peter 2
    Peter 2
    Patricia 5
    Patricia 5
    James 10

    An employee (Column A) records the amount of time spent on each individual task (Column B). If they have only worked on a single task, they will only have one row. If they have worked on six individual tasks, they will have six rows.

    There is no limit to the number of tasks they may have performed. Similarily, there is no special time value that they may have spent on any individual task. The number of different employees listed may also differ in different scenerios.

    What is the VBA macro that will sum all the hours spent by an employee on all his individual tasks, then leave only one row per employee?

    In the above example of input data, I would expect to see an output of:
    Jack 10
    Jill 10
    Peter 10
    Patricia 10
    James 10
    Many thanks in advance.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Loop to Sum Unknown Number of Rows

    Have you thought about using a pivot table?

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    10-15-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Loop to Sum Unknown Number of Rows

    Quote Originally Posted by Domski View Post
    Have you thought about using a pivot table?

    Dom
    I hadn't actually, no. However, in this case I do not think a pivot table is what is required.

    The above is a simplfied example of what I am trying to solve and will form but one function of a much more involved VBA application.

    The output will not be used for display or printing but will be subject to further processing and finally saved as an file for input to an overnight batch run.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop to Sum Unknown Number of Rows

    Here is one approach:
    Please Login or Register  to view this content.
    Last edited by StephenR; 10-15-2010 at 06:50 AM.

  5. #5
    Registered User
    Join Date
    10-15-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Loop to Sum Unknown Number of Rows

    Quote Originally Posted by StephenR View Post
    Here is one approach:

    Code example
    Thank you! This works beautifully in an example spreadsheet

    I just need know get to understand what it is doing and then integrate it into the wider application

    Thank you for your time and effort
    Last edited by DBExhale; 10-15-2010 at 06:55 AM. Reason: sp

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop to Sum Unknown Number of Rows

    Glad it worked. A more "Excel-native" approach might be to construct a list of unique names using Advanced Filter and then use the SUMIF formula.

    EDIT: like this. You need to add a header row.
    Please Login or Register  to view this content.
    Last edited by StephenR; 10-15-2010 at 09:01 AM.

+ 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