+ Reply to Thread
Results 1 to 3 of 3

How to sum some elements of another array into a new one

Hybrid View

  1. #1
    Registered User
    Join Date
    04-07-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    16

    How to sum some elements of another array into a new one

    Hi All,

    I have always used range references on worksheets for calculations, but am now starting to use arrays to work with data, due to how much faster they can be. I am trying to get "SMA_MIN_Array" to calculate a 50 period moving average of the preceding 50 values of the array named vArray. Does anybody know how to accomplish it? Here is what I have so far, and I cannot get it to work. The "Sum(vArray(j,j+49)/50) is the culprit. I know that isn't right, but I just don't know how to translate this into summing items 1 through 50 and dividing the total by 50. Any ideas? Here is a snippet of some code that I have tried: (If you live in Dallas, I will buy you a drink of your choice for help on this problem!)


    Thanks,
    Nick

    Dim Last_Row As Long
    Last_Row = Mainwb.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    Dim rng As Range
    Set rng = Mainwb.Worksheets("Sheet1").Range("F5:F" & Last_Row)
    
    Dim vArray() As Variant
    vArray = rng
    
    Dim i As Long
    For i = LBound(vArray, 1) To UBound(vArray, 1)
    Next
    
        Dim newWorkbook As Workbook
        Set newWorkbook = Application.Workbooks.Add
        
        With newWorkbook.Sheets("Sheet1")
        .Range(.Cells(5, 8), .Cells(Last_Row, 8)).Value = vArray
        End With
    
    
    Dim SMA_MIN_Array() As Variant
    ReDim SMA_MIN_Array(1 To Last_Row - 50, 1 To 1)
    For j = 1 To Last_Row - 50
    SMA_MIN_Array(j, 1) = Sum(vArray(j, j + 49))
    Next
    
    
    With newWorkbook.Sheets("Sheet1")
    .Range(.Cells(55, 10), .Cells(Last_Row, 10)).Value = SMA_MIN_Array
    End With

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to sum some elements of another array into a new one

    Notwithstanding your desire to use VBA I can't help thinking you're overcomplicating this if all you want is a 50 period moving average.

    Nothing works faster than standard Excel functions and hence as far as I can see all you need is

    Formula: copy to clipboard
    =Average(F1:F50)


    in say G 50 and copy this down adjacent to your column F values.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    04-07-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: How to sum some elements of another array into a new one

    I have to use arrays due to the sheer volume of the calculations. What you see is only the tip of the iceberg. I need to run hundreds of thousands of loop iterations with hundreds of thousands of calcs. Unfortunately I need to do this in arrays for the processing speed and flexibility. Any ideas? The more calcs I can do in memory without referencing a sheet the easier and faster this will be.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Array Storage Problem: Array Elements Deleted on 'End' Command
    By AidenS in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-15-2014, 10:38 AM
  2. [SOLVED] Removing specific elements from an Array based on matching elements in a different Array
    By jamesdeuce in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2014, 10:35 PM
  3. help assigning elements of dictionary to array and then pasting array into worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-05-2013, 10:37 AM
  4. [SOLVED] Populate Listbox with all rows of a dynamic array where elements of a single array match.
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-07-2012, 04:54 AM
  5. [SOLVED] Count elements in array
    By Jason Morin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-31-2005, 06:06 PM

Tags for this Thread

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