嵌套循环运行非常慢

时间:2019-02-18 19:52:06

标签: excel vba

Sub Rotate_Click()
    Dim i As Integer
    Dim j As Integer     
    Application.ScreenUpdating = False
    For i = 1 To 36
        For j = 1 To 36
            Cells(j + 40, 43 - i).Value = Cells(i + 2, j + 6).Value
       Next j
    Next i
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:2)

使用几个数组进行处理,这样就不会遍历工作表单元格。

Sub Rotate_Click()
    Dim i As long, j As long, val1 as variant

    val1 = cells(3, 7).resize(36, 36).value2
    redim val2(lbound(val1, 1) to ubound(val1, 1), _
               lbound(val1, 2) to ubound(val1, 2))

    For i = 1 To 36
        For j = 1 To 36
            val2(j, 37 - i) = val1(i, j)
       Next j
    Next i

    cells(41, 7).resize(ubound(val2, 1), ubound(val2, 2)) = val2

End Sub