+ Reply to Thread
Results 1 to 13 of 13

Sum portion of array without loop

  1. #1
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    Sum portion of array without loop

    Hi,

    I do hope the attached picture will be sufficient in order to help me.
    (I faced a problem to attach this small WB as an XLS).

    I'm looking for some way to some the red cells without looping.

    In this example the array was filled with A1:A10 values.
    In the real situation the array gets its values from other source than a Worksheet Range.

    As you can see I manged to transport the Array Values to F1:F10 and from here I could calculate the sum of F3:F8 but I do not want to use any helper columns.

    As you can see the wrong! sum command, I have used ,presents an 'Error 9'.

    Thanks, Elm
    Attached Images Attached Images

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    re: Sum portion of array without loop

    Please Login or Register  to view this content.
    The important line is the first one. It would tell you that C2 and D2 in your code are undeclared variables.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    re: Sum portion of array without loop

    Thanks, shg,

    Unfortunately, you must have missed the crucial line which states: "In the real situation the array gets its values from "other source than a Worksheet Range".

    Please Ignore Range A1:A10 and Range F1:F10 and try to find a solution from a point where the Array is already filled with those 10 values.

    Elm

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    re: Sum portion of array without loop

    If you can't possibly iterate the Array (for whatever reason) then temporarily place your Array into a worksheet object so you can use the method already outlined by shg.

  5. #5
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    re: Sum portion of array without loop

    Thanks, DonkeyOte,

    As you can see I manged to place all 10 values into range F1:F10 and from there I would have known how to sum a part of a that range.

    My intention is NOT to go through and/or use a Worksheet as an helper.

    If there is no way to accomplish my request with "pure" VBA commands without relaying on Worksheets or Worksheets Objects that would be preferable.

    However, as long as the solution will rum through something that is fully transparent to the user even if it needs a Worksheet Object - it will be fine - but as I'm not familiar with "worksheet objects" - please lighten up my eyes with a sample code.

    Thanks, Elm
    Last edited by ElmerS; 06-24-2009 at 11:44 AM.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    re: Sum portion of array without loop

    If you don't want to use a Worksheet to store the Array temporarily (such that you can sum quickly as per any standard range) then you will AFAIK need to iterate the Array... and I stress again, AFAIK...

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    re: Sum portion of array without loop

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    re: Sum portion of array without loop

    Thanks,

    Well..., I will "rephrase" the question although it seems to be the same "problem".

    Assume I have a 10 cells Array.

    How can I copy those red values (from cell 3-8 into cells 1-6 in a second Array - WITHOUT looping and without going through a Worksheet Range.

    Elm

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    re: Sum portion of array without loop

    What exactly is the problem with looping?
    Remember what the dormouse said
    Feed your head

  10. #10
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    re: Sum portion of array without loop

    Hi,

    Who mentioned anything about a looping problem.

    This is a part of a Technical High-school Exercise.

    AFAIR - those Exercises have never been too easy...

    Elm

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    re: Sum portion of array without loop

    You could familarize yourself with VBA's SafeArray data structure, use VarPtr to get the base address of the array, compute the offset to the data of interest, and use the CopyMemory API to copy the data to a new array.

    Happy hunting.

  12. #12
    Forum Contributor
    Join Date
    11-28-2008
    Location
    Reykjavík, Iceland
    MS-Off Ver
    Any of: 2003 & 2007
    Posts
    412

    re: Sum portion of array without loop

    "Piece of cake"

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

    re: Sum portion of array without loop

    Quote Originally Posted by ElmerS View Post
    This is a part of a Technical High-school Exercise.
    Oh, I see. I'll leave you to it then.

+ 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