简体   繁体   English

excel填充公式直到最后一行

[英]excel fill formula till last row

I have below vba code to put a formula in cell AE3 then copy down to last row, i have 5000+ rows but wonder why it take so long to process (about 5 min. and still running), is there a better way to do this? 我在vba下面的代码中将一个公式放在单元AE3中,然后向下复制到最后一行,我有5000多行,但想知道为什么要花这么长时间处理(大约5分钟并仍在运行),有没有更好的方法这个? i want copy down to last row as the list is dynamic data with different ranges every day. 我想复制到最后一行,因为列表是每天都有不同范围的动态数据。 Thanks. 谢谢。

 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

You should turn off both ScreenUpdating and Calculations when working with large numbers of formulas. 使用大量公式时,应同时关闭ScreenUpdating和Calculations。

This line If col_AE <> vbNullString Then isn't doing anything. 这行If col_AE <> vbNullString Then什么也不做。

Option Explicit 显式期权

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

The majority of the processing time is being used because the sheet is recalculating every time a formula is added. 正在使用大部分处理时间,因为每次添加公式时都会重新计算工作表。 I would just turn off ScreenUpdating and Calculations and replace all the formulas. 我只是关闭ScreenUpdating和Calculations并替换所有公式。 In this way I know that the formulas are consistent and that any errors introduced by users would be corrected. 通过这种方式,我知道公式是一致的,并且用户所引入的任何错误都将得到纠正。

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

This might speed it up - turn off the screen updating while it is running. 这可能会加快速度-在运行时关闭屏幕更新。

Application.ScreenUpdating = False
Application.ScreenUpdating = True

Please try this: 请尝试以下方法:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM