Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在自动填充函数中是否可以使用单元格而不是范围?_Vba_Excel_Excel 2010_Excel 2013 - Fatal编程技术网

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