Vba 在自动填充函数中是否可以使用单元格而不是范围?
如果我有这个,例如:Vba 在自动填充函数中是否可以使用单元格而不是范围?,vba,excel,excel-2010,excel-2013,Vba,Excel,Excel 2010,Excel 2013,如果我有这个,例如: Sub ExampleThing() Counter = 13 For i = 1 To Counter Range("A" & i) = Rnd Next Range("B1").Formula = "=(PI()*A1)" Range("B1").Select Selection.AutoFill Destination:=Range("B1:B" & Counter), Type:=x
Sub ExampleThing()
Counter = 13
For i = 1 To Counter
Range("A" & i) = Rnd
Next
Range("B1").Formula = "=(PI()*A1)"
Range("B1").Select
Selection.AutoFill Destination:=Range("B1:B" & Counter), Type:=xlFillDefault
End Sub
如果我想使用Cells()而不是Destination下的Range()呢?例如,将区域函数中的单元格引用替换为Cells(),如下所示:
Selection.AutoFill Destination:=Range("Cells(1,2):Cells(Counter,2)"), Type:=xlFillDefault
我一直在玩弄它,似乎无法让它发挥作用 你们很接近。下面是一个声明了变量的版本(您确实应该这样做),并删除了
Select
语句(这也是一个很好的做法):
Sub ExampleThing()
Dim Counter As Long
Dim i As Long
Counter = 13
For i = 1 To Counter
Range("A" & i) = Rnd
Next
Range("B1").Formula = "=(PI()*A1)"
Range("B1").AutoFill Destination:=Range(Cells(1, 2), Cells(Counter, 2)), Type:=xlFillDefault
End Sub