VBA – 检查公式是否在单元格运行缓慢

这是我正在看的。 我有一张使用以下VBA导入部分的工作表:

Sheets("Sheet1").Select Range("D1:D1").Select Selection.QueryTable.Refresh BackgroundQuery:=False 

这个问题是每当我运行它,它混淆了我在A和B列的公式。 所以我创build了一个子将检查如下:

 Sub fixAnB() Dim sh As Sheet9 Dim rw As Range Set sh = Sheet9 sh.EnableCalculation = False 'Sheets("Sheet1") Dim i As Long i = 0 Dim f1 As String, f2 As String For Each rw In sh.Rows i = i + 1 If i > 1 And sh.Cells(rw.Row, 4).Value <> "" Then f1 = "=CONCATENATE(G" & i & ",J" & i & ")" f2 = "=CONCATENATE(I" & i & ",H" & i & ",J" & i & ")" If sh.Cells(rw.Row, 1).Formula <> f1 Then sh.Cells(rw.Row, 1).Formula = f1 If sh.Cells(rw.Row, 2).Formula <> f2 Then sh.Cells(rw.Row, 2).Formula = f2 End If If sh.Cells(rw.Row, 4).Value = "" Then Exit For Next rw sh.EnableCalculation = True End Sub 

我的问题是,我可以在那张表上有两万到二十万条logging。 所以使用这个子程序来修复这些公式大约需要10 – 15分钟。 我正在寻找解决scheme之一:

  1. 一种解决原始问题的方法,以便在导入数据时不会混淆引用

或者2.一种使修补器子跑得更快的方法。

你觉得怎么样?

试试这个代码(在我的testing中,在200000行上花费less于0.5秒):

 Sub Test() t = Timer ' Application.ScreenUpdating = False ' Application.EnableEvents = False ' Application.Calculation = xlCalculationManual Dim sh As Worksheet Set sh = ActiveSheet Dim i As Long i = sh.Range("D1").End(xlDown).Row sh.Range("A2:A" & i).Formula = "=CONCATENATE(G2,J2)" sh.Range("B2:B" & i).Formula = "=CONCATENATE(I2,H2,J2)" ' Application.ScreenUpdating = True ' Application.EnableEvents = True ' Application.Calculation = xlCalculationAutomatic MsgBox Timer - t End Sub