+ Reply to Thread
Results 1 to 13 of 13

Sum portion of array without loop

Hybrid View

  1. #1
    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.

  2. #2
    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...

  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,

    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

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

    re: Sum portion of array without loop

    What exactly is the problem with looping?
    Everyone who confuses correlation and causation ends up dead.

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

    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.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    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"

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

    re: Sum portion of array without loop

    Option Explicit
    
    Sub x()
        Dim v1 As Variant
        Dim iBeg As Long
        Dim iEnd As Long
        
        ' however v1 gets initialized ...
        v1 = Range("A1:A10").Value
        iBeg = Range("C2").Value
        iEnd = Range("D2").Value
        
        ' then ...
        With Range("B1").Resize(UBound(v1)) ' or any other convenient place
            .Value = v1
            MsgBox WorksheetFunction.Sum(Range(.Cells(iBeg), .Cells(iEnd)))
        End With
    End Sub

+ 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