提高macros循环的速度,在c上进行数据转换。 76,000行数据

我已经尝试了两种不同的configuration电子表格工具的方法,我已经将客户端提供的数据转换为我们自己的数据代码。

数据集可能会变得特别大(这是76,335行),所以macros观性能是非常重要的。

我尝试的第一种方法是将一系列索引/匹配公式logging到VBA中,并将这些代码插入到M到Y列,然后向下拖动到原始数据集中最后一行的位置(位于列A到J) 这些公式中只有一个是数组公式,我已经尝试了没有数组公式的代码,总体收益很小。 这种方法(我们称之为方法A)花费了14分8秒(带有i7-2600 CPU和8 GB RAM的Windows PC)。

如果我用VBA做excel公式所做的计算,我认为它会提高性能,我运行了下面的循环(方法B)。

Application.Calculation = xlCalculationManual Application.ScreenUpdating = False Application.EnableEvents = False lastRow = ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Row For i = 6 To lastRow With ThisWorkbook.Worksheets("CONVERSION") If Range("M" & i) = "BIN" Then Range("N" & i) = "Duplicate" Else Range("N" & i) = Range("B" & i) & "-" & Range("D" & i) & "-" & Range("M" & i) End If If Range("B" & i) = "Duplicate" Then Range("O" & i) = "D" Else Range("O" & i) = "N" End If If Range("E2") = "Groundwater" Then Range("P" & i) = "WG" ElseIf Range("E2") = "Leachate" Then Range("P" & i) = "LE" ElseIf Range("E2") = "Surface water" Then Range("P" & i) = "WS" Else Range("P" & i) = "Other" End If 'Plus another six If statements similar to the above to populate Cols P to Y... Not included here to keep this code on StackOverflow easier to read End With Next i Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Application.EnableEvents = True 

这令人吃惊的花了27分27秒。 这是尽pipe有Application.Calculation = xlCalculationManualApplication.ScreenUpdating = FalseApplication.EnableEvents = False

为什么VBA计算方法比较慢? 这是我的循环效率低下的事情吗? 难道这是因为VBA不得不反复回到电子表格的值,来回呢?

一般的指导和意见,将不胜感激。 我知道我有一个大的数据集,但我希望能够运行这个转换代码比14分钟更快。

你需要使用数组 – 我已经切换你的代码,并将所有内容都popup到数组中,以完成大部分的工作:

 Sub SpeedUp() Dim iLastRow As Long, iLastCol As Long, i As Long Dim Arry() As Variant Application.Calculation = xlCalculationManual Application.ScreenUpdating = False Application.EnableEvents = False iLastRow = ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Row iLastCol = ActiveSheet.Cells(5, Columns.Count).End(xlToLeft).Column 'I'm assuming Row 1 is the header row. Also, if you're creating colums (to column Y) then just change this to "iLastCol = 25" ReDim Arry(1 To iLastRow, 1 To iLastCol) ' This array is a 2 dimensionnel array and you can referance rows/columns within it like you would using Cells([Row number],[Column number]) Arry = Range(Cells(1, 1), Cells(iLastRow, iLastCol)) For i = 6 To iLastRow If Arry(i, 13) = "BIN" Then '13 corresponds to column M Arry(i, 14) = "Duplicate" Else Arry(i, 14) = Arry(i, 2) & "-" & Arry(i, 4) & "-" & Arry(i, 13) End If If Arry(i, 2) = "Duplicate" Then Arry(i, 15) = "D" Else Arry(i, 15) = "N" End If If Arry(i, 5) = "Groundwater" Then Arry(i, 16) = "WG" ElseIf Arry(i, 5) = "Leachate" Then Arry(i, 16) = "LE" ElseIf Range("E2") = "Surface water" Then Arry(i, 16) = "WS" Else Arry(i, 16) = "Other" End If 'Plus another six If statements similar to the above to populate Cols P to Y... Not included here to keep this code on StackOverflow easier to read Next i ThisWorkbook.Worksheets("CONVERSION").Range(Cells(1, 1), Cells(iLastRow, iLastCol)) = Arry ' Sets all the values Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Application.EnableEvents = True End Sub