过滤数据然后将公式插入可见单元格的宏

Macro to filter data and then insert formula onto visible cells

我正在尝试过滤工作表(通过列 DL)。然后,我需要将一个公式插入 Column DQ,但仅限于可见单元格。我之前使用的是下面的代码,除了我不想要对单元格 DQ3 的特定引用之外。此单元格可以更改,因此会复制并粘贴错误的公式。

Dim LastRow As Long
Dim FinalRow As Long

LastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("DL2:DL" & LastRow).AutoFilter Field:=116, Criteria1:= _
    "ABC"

Range("DQ3").Copy

FinalRow = Cells(Rows.Count, "B").End(xlUp).Row
Range("DQ3:DQ" & FinalRow).Select
ActiveSheet.Paste

我尝试了下面的代码,但它 returns 出现“运行-time error '1004': 最后一行代码出现应用程序定义或对象定义的错误:

Dim LastRow As Long
Dim FinalRow As Long

LastRow = Range("B" & Rows.Count).End(xlUp).Row

Range("DL2:DL" & LastRow).AutoFilter Field:=116, Criteria1:= _
    "ABC"

FinalRow = Range("B" & Rows.Count).End(xlUp).Row
Range("DQ2:DQ" & FinalRow).FormulaR1C1 = "=(RC[-1]-RC[-2])"

我也试过这个代码:

 Dim LastRow As Long
 Dim FinalRow As Long


LastRow = Range("B" & Rows.Count).End(xlUp).Row

Range("DL2:DL" & LastRow).AutoFilter Field:=116, Criteria1:= _
    "ABC"


FinalRow = Range("B" & Rows.Count).End(xlUp).Row
Set RNG = Range("DQ2:DQ" & FinalRow).SpecialCells(xlCellTypeVisible)

RNG = "=(RC[-1]-RC[-2])"

这运行没有任何错误,但没有将任何数据填充到列 DQ。

关于如何消除错误或如何实现我最初的目标有什么建议吗?我不确定我正在尝试的方法是否会奏效,但这就是我卡住的地方。

谢谢!

我能够使用下面的代码实现我的目标。发帖以防其他人将来遇到同样的问题。

With ActiveSheet.Range("DQ2:DQ" & Cells(Rows.Count,2).End(xlUp).Row).SpecialCells(xlCellTypeVisible)
  .Cells.FormulaR1C1 = "=((RC[-1]-RC[-2])"`
  .Cells.FillDown`
   Worksheets("WorksheetName").Columns(10).Calculate

End With

干杯!