这是我正在看的内容。我有一张表,我使用以下VBA导入部分:
Sheets("Sheet1").Select
Range("D1:D1").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
这个问题就是每当我运行它时,它会混淆我在A和B列中的公式。所以我创建了一个sub来检查它,如下所示:
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
我的问题是,我可以在该表上有20,000到200,000条记录。因此使用sub来修复公式大约需要10-15分钟。我正在寻找解决方案之一:
或 2.一种使修复程序子运行速度更快的方法。
你觉得怎么样?
答案 0 :(得分:1)
尝试使用此代码(我的测试在200000行上花费不到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