方法1:不使用for迴圈,改以「System.Collections.ArrayList」作法
Sub 方法1不使用for迴圈() Dim i As Long, count As Long Dim coll As Object Dim a Dim t As Long count = Sheets(1).Cells(1, 2) t = Timer Sheets(1).Rows("3:65535").Clear For i = 1 To count Sheets(1).Cells(i + 2, 1) = i Next a = Sheets(1).Cells(3, 1).Resize(count).Value Set coll = CreateObject("System.Collections.ArrayList") With coll '清除前一次操作的暫存,做加速用 .Clear '只能用字串進行反轉 For Each q In a .Add CStr(q) Next '反轉 .Reverse Sheets(1).Cells(3, 2).Resize(count, 1) = Application.Transpose(.ToArray) Sheets(1).Cells(3, 3).Resize(1, count) = .ToArray End With Set coll = Nothing MsgBox Format(Timer - t, "0.00") & "秒" End Sub方法2:使用for迴圈
Sub 方法2使用for迴圈() Dim i As Long, count As Long Dim a, b() Dim t As Long count = Sheets(1).Cells(1, 2) t = Timer Sheets(1).Rows("3:65535").Clear For i = 1 To count Sheets(1).Cells(i + 2, 1) = i Next a = Sheets(1).Cells(3, 1).Resize(count).Value ReDim b(count - 1) For i = count To 1 Step -1 b(count - i) = a(i, 1) Next Sheets(1).Cells(3, 2).Resize(count, 1) = Application.Transpose(b) Sheets(1).Cells(3, 3).Resize(1, count) = b Erase b MsgBox Format(Timer - t, "0.00") & "秒" End Sub程式碼執行畫面
檔案連結。
沒有留言:
張貼留言