如何在 excel 中相对自动填充

How to autofill relatively in excel

我试过制作一个 VBA,用信息填充单元格。问题是我想相对地定义范围。我做了一个函数来计算自动填充过程应该何时结束。所以范围 Range("f3:f5") 应该是 Range("f3:D2")

我附上了我的电子表格的图片,希望它能让一切更容易理解。

Sub myautofill()
Range("F3").Value = "=R[-1]C+1"
Range("F3").Select
Selection.AutoFill Destination:=Range("f3:f5")
End Sub

enter image description here

您可以通过多种方式使用 D2 的内容,例如 Cells or Range

我更喜欢范围,你可以这样做:

Selection.AutoFill Destination:=Range("f3:" & Range("D2").Value)

结果将是从 F3 到您在 D2 中输入的任何内容的范围,在您的屏幕截图的情况下将是 F12。意味着编译器会这样解释它:

Selection.AutoFill Destination:=Range("f3:f12")