在循环中粘贴 Excel 公式

Paste Excel Formula in the Loop

我想将这个公式 =$F-E1 粘贴到单元格范围 A1A10,所以它应该是:

For A1 --> =$F-E1
For A2 --> =$F-E2
For A3 --> =$F-E3
For A4 --> =$F-E4
...
For A10 --> =$F-E10

第一部分已修复,另一部分未修复。

很简单,创建一个过程,在其中为指定范围内的每个单元格执行一些操作。

Option Explicit
Private Sub fill_formula(where As Range)
    Dim cell As Range
    Dim i As Long
    i = 1
    For Each cell In where
        cell.Formula = "=$F - E" & i
        i = i + 1
    Next cell
End Sub

可以调用 的样式(显然你可以传递一个变量)

Private Sub main()
    fill_formula (Sheets("Your sheet name").Range("A1:A10"))
End Sub

相当容易。
虽然我不明白为什么这不会比手动拖动公式更容易,特别是考虑到这是引用静态范围(E1E[max]

我知道这已经得到解答,但无需循环即可轻松完成

With Sheet1
    .Range(.Cells(1, 1), .Cells(10, 1)).Formula = "=$F-E1"
End With

给出相同的结果