+ Reply to Thread
Results 1 to 4 of 4

Summing Like Element Array Values

  1. #1
    DMS
    Guest

    Summing Like Element Array Values

    All,

    I am trying to come up with a way to identify and then sum like Elements in
    an Array.

    For Example, Lets Say I have an array that contains the following items
    1.First Name
    2.Last Name
    3.Hours worked

    The Macro I am witing is adding this type of data to an Array, but I don't
    want there to be duplicate array items for the same First and Last Name.

    So lets say the first item in my Array is John, Smith, 20. Before I add the
    next item to the Array, I want to make sure that the First and Last name
    aren't already in the Array, if it is already in the Array, then it should
    add the hours to the existing Array item.

    Or, is there a way to sum like elements after an Array is created?

    Thanks for your help.

    DMS

  2. #2
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    Before you add the item to the array you could compare the item to be added to each element in the array by looping through the array. If the match for both first and last name is found then reassign the third element to the current value plus the new value.
    If there is no match then you can add each item to the array.

  3. #3
    Microsoft
    Guest

    Re: Summing Like Element Array Values

    have you thought of using a collection instead?

    also, be sure that the key is the last + "!" + first, not just last-first.

    Collections are faster to work with than arrays and it is instantly obvious
    whether something is in a collection (just look for a trappable error when
    trying to ad to it!) instead of looping through an array's elements.

    HTH...
    Bill
    "DMS" <[email protected]> wrote in message
    news:[email protected]...
    > All,
    >
    > I am trying to come up with a way to identify and then sum like Elements
    > in
    > an Array.
    >
    > For Example, Lets Say I have an array that contains the following items
    > 1.First Name
    > 2.Last Name
    > 3.Hours worked
    >
    > The Macro I am witing is adding this type of data to an Array, but I don't
    > want there to be duplicate array items for the same First and Last Name.
    >
    > So lets say the first item in my Array is John, Smith, 20. Before I add
    > the
    > next item to the Array, I want to make sure that the First and Last name
    > aren't already in the Array, if it is already in the Array, then it should
    > add the hours to the existing Array item.
    >
    > Or, is there a way to sum like elements after an Array is created?
    >
    > Thanks for your help.
    >
    > DMS




  4. #4
    RB Smissaert
    Guest

    Re: Summing Like Element Array Values

    A collection of arrays might be the best solution, but what if there are
    people
    with the same first name and surname or was that just a simplified example?

    RBS


    "DMS" <[email protected]> wrote in message
    news:[email protected]...
    > All,
    >
    > I am trying to come up with a way to identify and then sum like Elements
    > in
    > an Array.
    >
    > For Example, Lets Say I have an array that contains the following items
    > 1.First Name
    > 2.Last Name
    > 3.Hours worked
    >
    > The Macro I am witing is adding this type of data to an Array, but I don't
    > want there to be duplicate array items for the same First and Last Name.
    >
    > So lets say the first item in my Array is John, Smith, 20. Before I add
    > the
    > next item to the Array, I want to make sure that the First and Last name
    > aren't already in the Array, if it is already in the Array, then it should
    > add the hours to the existing Array item.
    >
    > Or, is there a way to sum like elements after an Array is created?
    >
    > Thanks for your help.
    >
    > DMS



+ 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