Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

was looking for a solution to this but couldnt find a good explanation.

I currently have code that returns an array for each iteration into a row of a spreadsheet. On the last iteration it take all the row data and copies and pastes it to another sheet. I know it would be a million times faster if I could just make one huge array with each item being a array of data and then transpose it without ever copying and pasting / manipulating cells at all.

How can I do this? The bigger issue for me is also that each major iteration is based off a global variable and is not a loop within the sub itself. This is because the sheet data that is used for making the array for each iteration requires time to load into the sheet.

Here is the code for my sub. As of now you can see that at the each it write the data the Range("A" & pos), which then increments a counter on the sheet itself. Once the sub finishes, the global variable iterates and it assumes again... So in short the array or arrays would need to be a global variable

*Before anyone looks and says F I'm not looking at all that code, the only important part really is like the last 10 lines... everything else is just for the sake of clarity.

Sub find_patternRevised()

Application.ScreenUpdating = False


Sheets("SingleEquityHistoryHedge").Activate

Range("A47:M47").Clear

Dim strt_pt() As Long
Dim end_pt() As Long

Dim i As Long
Dim j As Long
Dim k As Long
Dim y As Long
Dim pos As Long

pos = Range("F1").value


k = 0

For j = 8 To 12

    i = 13

    ' find start points for each column

    Do While Not IsNumeric(Cells(i, j).value)
        i = i + 1
    Loop

        ReDim Preserve strt_pt(4)
        strt_pt(k) = i

        'Debug.Print strt_pt(k)

        k = k + 1
Next j

k = 0


i = 13
j = 8



' finds patterns for each column

Do While j <= 12

    ' find start points for each column
    If Cells(strt_pt(k), j).value > 0 Then
        If Not IsNumeric(Cells(i, j)) Then
            i = i + 1
        Else

            On Error Resume Next ' bypass error thrown by #VALUE
            'loop until return sign changes or cell is blank
            Do Until Cells(i, j).value < 0 Or Cells(i, j).value = vbNullString
                i = i + 1
            Loop

            ReDim Preserve end_pt(5)
            end_pt(y) = i

            'Debug.Print end_pt(y)
            y = y + 1
            j = j + 1
            i = 13 'reset start after entering value

        End If
    ElseIf Cells(strt_pt(k), j).value < 0 Then

        If Not IsNumeric(Cells(i, j)) Then
            i = i + 1
        Else

            On Error Resume Next
            Do Until Cells(i, j).value > 0 Or Cells(i, j).value = vbNullString
                i = i + 1
            Loop

            ReDim Preserve end_pt(5)
            end_pt(y) = i

            'Debug.Print end_pt(y)
            y = y + 1
            j = j + 1
            i = 13 ' reset start after entering value
        End If

    End If




 Loop



Dim lent As Long
Dim end_ct As Long


end_ct = 0

Dim final_array() As Variant
ReDim Preserve final_array(11)


final_array(0) = Range("B2").value
final_array(1) = Range("B3").value

j = 8


For lent = 2 To 11 Step 1


    If lent Mod 2 = 0 Then

        ReDim Preserve final_array(11)
        final_array(lent) = end_pt(end_ct) - strt_pt(end_ct)

    Else

        'gets average over pattern period
        Dim avg_rng As Range
        Set avg_rng = Sheets("SingleEquityHistoryHedge").Range(Cells(strt_pt(end_ct), j), Cells(end_pt(end_ct) - 1, j))

        Dim avg_value As Double
        avg_value = avgVal(avg_rng)

        ReDim Preserve final_array(11)
        final_array(lent) = avg_value

        end_ct = end_ct + 1
        j = j + 1
    End If



Next lent

Range("A" & pos).Resize(1, UBound(final_array) + 1) = final_array

Sheets("SingleEquityHistoryHedge").Range("f1").value = pos + 1



End Sub

share|improve this question
    
Range("A" & pos).Resize(1, UBound(final_array) + 1) = final_array This is not what I gave you last time :) –  Siddharth Rout Sep 28 '13 at 5:23
    
haha yeah i realized i dont need to transpose for my purposes –  googlekid Sep 28 '13 at 9:55

1 Answer 1

It looks like your arrays are only one dimensional. YOu need to create 2-d arrays, populate it all, and then you can write it to the sheet with a single statement.

Here is an example, you will obviously need to tailor it to your needs (I said "F, I'm not looking through all that code!!!")

Sub foo()
Dim arr(1 To 10, 1 To 5)  'creates an array 10 rows x 5 columns
Dim x As Long
Dim y As Long

For x = LBound(arr, 1) To UBound(arr, 1)
    For y = LBound(arr, 2) To UBound(arr, 2)
        arr(x, y) = Application.WorksheetFunction.RandBetween(1, 100)
    Next
Next

'Put it on the sheet:
Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr


End Sub
share|improve this answer
    
is there any way to write an entire line in one statement? Or must I loop through the array for each column and row? –  googlekid Sep 28 '13 at 9:54
    
also i want to declare this array as a global variable... how can i redimension it within the subroutine? –  googlekid Sep 28 '13 at 10:02
    
You can write an entire line I think with the index function. Redimensioning a global variable is the same as a local variable. But why do you think this needs to be a global variable ? –  David Zemens Sep 28 '13 at 12:48
    
Do you mean "write a line" to the array or from the array (to the sheet)? –  David Zemens Sep 28 '13 at 12:53
    
I mean as in if an array is val(11,count).;.. ie row of 11 elements with the second dimension being the number of iterations –  googlekid Sep 28 '13 at 13:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.