我有一个VBA脚本,可以将SoapUI - Single
工作表中的行数据复制到STpremcalc
工作表,然后将最终计算复制回SoapUI - Single
工作表。它工作正常,但我有10000行数据,一行需要大约30秒。当我用1000行测试它时,它在一分钟内完成。
造成这种情况的原因是什么?是因为VBA脚本在读取整个工作表之前是否复制了值。
Sub SingleRating()
Dim i As Long
Dim iteration As Variant
Dim seleciton As Variant
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("SoapUI - Single")
Set ws2 = Worksheets("STpremcalc")
iteration = 0
iteration = InputBox("Please Select Row Iteration", "", "1000")
seleciton = iteration + 2
For i = 3 To seleciton
ws2.Range("B3").Value = ws1.Range("B" & i).Value
ws2.Range("B4").Value = ws1.Range("C" & i).Value
ws2.Range("B5").Value = ws1.Range("D" & i).Value
ws2.Range("B6").Value = ws1.Range("E" & i).Value
ws2.Range("E3").Value = ws1.Range("F" & i).Value
ws2.Range("E4").Value = ws1.Range("G" & i).Value
ws2.Range("E5").Value = ws1.Range("H" & i).Value
ws2.Range("E6").Value = ws1.Range("I" & i).Value
ws2.Range("G3").Value = ws1.Range("J" & i).Value
ws2.Range("G4").Value = ws1.Range("K" & i).Value
ws2.Range("G5").Value = ws1.Range("L" & i).Value
ws2.Range("J3").Value = ws1.Range("N" & i).Value
ws2.Range("J4").Value = ws1.Range("O" & i).Value
ws2.Range("J6").Value = ws1.Range("P" & i).Value
ws2.Range("B9:E9").Value = ws1.Range("Q" & i, "T" & i).Value
ws2.Range("B10:E10").Value = ws1.Range("U" & i, "X" & i).Value
ws2.Range("B11:E11").Value = ws1.Range("Y" & i, "AB" & i).Value
ws2.Range("B12:E12").Value = ws1.Range("AC" & i, "AF" & i).Value
ws2.Range("B13:E13").Value = ws1.Range("AG" & i, "AJ" & i).Value
ws2.Range("B14:E14").Value = ws1.Range("AK" & i, "AN" & i).Value
ws2.Range("B15:E15").Value = ws1.Range("AO" & i, "AR" & i).Value
ws2.Range("B16:E16").Value = ws1.Range("AS" & i, "AV" & i).Value
'''''''''''''''''''''''''''''''''''''''''''''''''
ws1.Range("AW" & i).Value = ws2.Range("M4").Value
ws1.Range("AX" & i).Value = ws2.Range("M5").Value
ws1.Range("AY" & i).Value = ws2.Range("M6").Value
Application.StatusBar = "Current iteration: " & (i - 2) & "/" & iteration
Next i
End Sub
答案 0 :(得分:3)
如果这是您的整个代码,我建议在初始化变量后插入此代码:
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
calcState = Application.Calculation
eventsState = Application.EnableEvents
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
在代码的最后(End Sub上方)将其反转:
Application.ScreenUpdating = screenUpdateState
Application.DisplayStatusBar = statusBarState
Application.Calculation = calcState
Application.EnableEvents = eventsState
根据我的经验,特别是ScreenUpdating部分在复制/插入行时提供了巨大的性能提升。如果在禁用它后仍然存在性能问题,我们需要查看实现本身。
我认为这应该对你有所帮助,因为我在工作表之间复制了数万行而且从未出现性能问题。