我有以下vba代码将公式放入单元格AE3然后复制到最后一行,我有5000多行,但想知道为什么需要这么长时间来处理(大约5分钟并仍在运行),是否有更好的这样做的方法?我希望复制到最后一行,因为列表是每天具有不同范围的动态数据。感谢。
Sub FillRows()
Dim col_AE As String
Sheet1.Select
col_AE = "=IFERROR(INDEX(setting!C[-17],MATCH(smart!RC[-9],setting!C[-18],0)),"""")"
If col_AE <> vbNullString Then
For j = 3 To Range("A" & Rows.Count).End(xlUp).Row - 1
If Range("ae" & j).Value = vbNullString Then
Range("ae" & j).Value = col_AE
End If
Next j
End If
End Sub
答案 0 :(得分:1)
使用大量公式时,应关闭ScreenUpdating和Calculations。
这一行If col_AE <> vbNullString Then
没有做任何事情。
选项明确
Sub FillRows()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim col_AE As String
With Sheet1
For j = 3 To .Range("A" & .Rows.Count).End(xlUp).Row - 1
If .Range("ae" & j).Value = vbNullString Then
.Range("ae" & j).FormulaR1C1 = "=IFERROR(INDEX(setting!C[-17],MATCH(smart!RC[-9],setting!C[-18],0)),"""")"
End If
Next j
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
正在使用大部分处理时间,因为每次添加公式时都会重新计算工作表。我将关闭ScreenUpdating和Calculations并替换所有公式。通过这种方式,我知道公式是一致的,并且用户引入的任何错误都将得到纠正。
Sub FillRows2()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim col_AE As String
With Sheet1
.Range("A3", "A" & .Rows.Count).End(xlUp).FormulaR1C1 = "=IFERROR(INDEX(setting!C[-17],MATCH(smart!RC[-9],setting!C[-18],0)),"""")"
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
这可能会加快速度 - 在屏幕更新运行时关闭它。
Application.ScreenUpdating = False
Application.ScreenUpdating = True
答案 2 :(得分:0)
请试试这个:
Option Explicit
Sub fillFormula()
Dim wbk1 As Workbook
Dim lastRow As Long
Set wbk1 = ActiveWorkbook
With wbk1.Sheets("sheet1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
lastRow = lastRow - 1
.Range("AE3:AE" & lastRow).Formula = _
"=IFERROR(INDEX(setting!C[-17],MATCH(smart!RC[-9]," _
& "setting!C[-18],0)),"""")"
End With
End Sub