2014-12-08 148 views
0

如何更快地設置我的循環?真的很慢For循環

我有4列和超過250行,它需要像5分鐘循環這樣的:從

XML
Application.Calculation = xlCalculationManual 
Call CPU0 

Dim TableA As Range: Set TableA = Range("L_PriceA") 

With TableA 
LongA = TableA.Rows.Count 
    For s = 1 To LongA 
     .Cells(s, 3).Value = "dynamic" 
     .Cells(s, 4).Value = "dynamic" 
     .Cells(s, 5).Value = "dynamic" 
     .Cells(s, 6).Value = "dynamic" 
    Next 
End With 

「動態」將是數據,因爲沒有人幫我THIS,我不知道:/。還看了一下stackO中的一些循環問題,但沒有主要提升。

回答

1

在一個範圍內循環很慢。通過將數據移動到Variant數組來避免它,如下所示:

Dim dat As Variant 
Dim s As Long 

With TableA 
    LongA = .Rows.Count 
    dat = .Value ' TableA must be at least 6 columns wide 
    For s = 1 To LongA 
     dat(s, 3) = "dynamic" 
     dat(s, 4) = "dynamic" 
     dat(s, 5) = "dynamic" 
     dat(s, 6) = "dynamic" 
    Next 
    TableA.Value = dat 
End With 
+0

'dat(s,3).Value ='是一個錯字。你的意思是'dat(s,3)='對嗎? – ja72 2014-12-08 01:53:17

+0

@ ja72你是對的,謝謝 – 2014-12-08 07:48:14

+0

omg 200%更好謝謝verymuch =) – 2014-12-08 22:06:44