简体   繁体   中英

VBA Array Faster Output to Excel Sheet

Is there a faster version method to output an array to excel?

I currently have data stored in 3 arrays output_dt, output_s1, output_s2 .

Format sample:

15/12/2017  2165    2170
15/12/2017  2165    2175
15/12/2017  2165    2180
15/12/2017  2165    2185
15/12/2017  2165    2190
15/12/2017  2165    2195
15/12/2017  2165    2200
15/12/2017  2165    2205
15/12/2017  2165    2210

My current working solution is this:

Application.ScreenUpdating = False
Worksheets("Strategies").Columns(4).ClearContents
Worksheets("Strategies").Columns(5).ClearContents
Worksheets("Strategies").Columns(6).ClearContents
[d1].Resize(UBound(output_dt)) = Application.Transpose(output_dt)
[e1].Resize(UBound(output_s1)) = Application.Transpose(output_s1)
[f1].Resize(UBound(output_s2)) = Application.Transpose(output_s2)
Application.ScreenUpdating = True

However, this is painfully slow, even for just 150 rows .

Any methods to speed this up?

The loop I wrote for this to loop through the list of dates and 2 numbers to create a new set of data, creating 3x single 1D arrays.

    For i = LBound(data) To UBound(data)
        For j = LBound(data) To UBound(data)
            If data(i, 1) = data(j, 1) Then
                If data(i, 2) < data(j, 2) Then
                    x = x + 1
                    'MsgBox data(i, 2) & "-" & data(j, 2)
                    ReDim Preserve output_dt(0 To x + 1)
                    ReDim Preserve output_s1(0 To x + 1)
                    ReDim Preserve output_s2(0 To x + 1)
                    output_dt(x) = data(i, 1)
                    output_s1(x) = data(i, 2)
                    output_s2(x) = data(j, 2)
                End If
            End If
        Next j
    Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM