Results 1 to 8 of 8

Thread: How to return data from a complex nested populated VBA Array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Regular
    Joined
    May 2017
    Posts
    19
    Location
    Hi. Sorry just re-read and looks like my post got all jumbled up so here it is again but this in English!

    I have some basic knowledge of how to create and store data in a vba array thanks to help on here in the past. However I have now encountered a new challenge/obstacle I've never seen before. Basically I have created an array to store results from a UDF calculation for a range of data variables (columns) and return the results of the summary array to a spreadsheet in one go. However it appears Excel is creating tens of sub arrays within the main arrays and so I am unable to get the results out to a spreadsheet. My code is below and here are the main components are:

    Stage 1) This works and simply loops through columns of data in the Sheet("Dataraw") and place them into the "calculation" sheet one at a time. The column name is in cell U14 followed by the rest of the data in that column.

    Stage 2) Here I create an array [Results(4 To 30, 1 To 2)] to store the results for each variable/column that is looped from Column 4 to 30. The first column in the array stores the column name and this part works fine. The second column of the array stores the results from a UDF that takes each variable as an input and outputs stats - denoted here by custom function:

    Results(i, 2) = customfunction(x)

    However the UDF generates 4 output values for each variable in one go and because of this (I think), VBA is creating sub arrays to store the results but creates lots of empty arrays in the process. (See attachment). So for example in the VBA locals windows for the first column in the array (i,1), I see as expected:

    (Results 4)
    (Results 4,1) column name. 'All good. Then I see;
    (Results 4,2) nothing

    When I click the drill down box relating to (Results 4,2) I see:
    (Results 4,2)(1,1) populated value
    (Results 4,2)(1,2) populated value
    (Results 4,2)(1,3) populated value
    (Results 4,2)(1,4) populated value

    But it then seems VBA creates other empty sub arrays that are not needed that follow the same structure as above, i.e
    (Results 4,2)(2) null
    (Results 4,2)(2,1) null
    (Results 4,2)(2,2) null
    (Results 4,2)(2,3) null
    (Results 4,2)(2,4) null

    And this empty arrays repeat up to Results (5,2). And then (Results 5) are populated with the next variable and the above pattern repeats itself - See attached image

    My question is: 1) How do I get this data out of the array into a spreadsheet? The following code only returns the column headings from (Results 4,1) to (Results 30,1) but none of the calculation results from (Results 4,2)(1,1) to (Results 4,2)(1,1)?
    Sheets("calculation").Range("AY2:BB32") = Application.transpose.Results()

    I've tried using index function but can't get it to work.

    2) My second question is instead of unintentionally creating all these empty sub arrays is there a way to create just 5 storage points per variable/column or (5*30) total points to capture the data and return it to an Excel sheet?
    For example in the past I would just create an array of results (4 to 30, 1 to 5) to store results for each variable (i) like the following but this doesn't work and it just creates even more empty arrays:

    Results (i, 1) = variable name (i, 1)
    Results (i, 2) output 1 (i, 2)
    Results (i, 3) output 2 (i, 3)
    Results (i, 4) output 3 (i, 4)
    Results (i, 5) output 4 (I,5)

    Would really appreciate some help with this as it really puzzling!

    Thanks

    Whisky


    Sub Exampleofnestedarray()
    ‘STAGE 1
    Dim Startrange As Long, ColNum As Long, lastrow As Long
    Dim outvariables As Integer
    Dim x As Range
    Dim sortcol As Integer
    Dim sortdirect As String
     lastrow = Range("B15").End(xlDown).row
     lastcol = Range("R14").End(xlToLeft).Column 'address bring back the cell address, splits on takes the letter from address
     Startrange = 14
     ColNum = Range("R14").End(xlToLeft).Column - 1 'determines start
         Sheets("Dataraw").Select
       z = Sheets("Dataraw").Range("C1").End(xlToRight).Column
       For i = 4 To z 'for each column of data/series
        Sheets("calculation").Range("U14:U" & lastrow).Value = Sheets("Dataraw").Range(Cells(1, i), Cells(lastrow, i)).Value
     
    '===STAGE2
    'CREATE ARRAY TO Store Results from array function that returns four separate outputs (by column)
    Dim Results(4 To 30, 1 To 2) As Variant
    Results(i, 1) = Sheets("calculation").Range("U14").Value
    Results(i, 2) = customfunction(x)
    Next i
    '==STAGE 3 - return results array data to sheet
    Sheets("calculation").Range("AY2:BB32") = Application.transpose.Results()
    End Sub
    Attached Images Attached Images

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •