您可以在自动填充功能中使用单元格而不是范围吗? [英] Can you use Cells instead of Range inside of an Autofill function?

查看:61
本文介绍了您可以在自动填充功能中使用单元格而不是范围吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有这个

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()怎么办?就像,不要在range函数中包含单元格引用,而是将其替换为Cells(),如下所示:

What if, instead of using Range() under Destination, I wanted to use Cells()? Like, instead of having cell references inside the range function, replace that with Cells(), like this:

Selection.AutoFill Destination:=Range("Cells(1,2):Cells(Counter,2)"), Type:=xlFillDefault

我一直在玩它,似乎无法使其正常工作.

I've been playing around with it, and can't seem to get it to work.

推荐答案

您非常亲密.这是一个声明了变量的版本(实际上应该这样做),并且消除了Select语句(也是一种好习惯):

You're very close. Here's a version with your variables declared (which you really should do) and the Select statements eliminated (also a good practice):

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

这篇关于您可以在自动填充功能中使用单元格而不是范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆