vba 根据相邻列自动填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17822756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:13:40  来源:igfitidea点击:

autofill down according to adjacent column

vbaexcel-vbaexcel

提问by user2612443

I'm looking for VBA code that will autofill data down according to the length of an adjacent column. I know there are a few ways to go about this, but which is best?:

我正在寻找将根据相邻列的长度自动填充数据的 VBA 代码。我知道有几种方法可以解决这个问题,但哪种方法最好?:

If LastRow > Selection.Row Then
   Range("D2").AutoFill Destination:=Range("D2:D" & LastRow)

or something like:

或类似的东西:

If Not IsEmpty(ActiveCell.Offset(0,1)) Then
   Range("D2").AutoFill Destination:=Range("D2:D" & LastRow)

I'm pretty sure neither of these work exactly how I want it so what am I missing?

我很确定这些都不是我想要的,所以我错过了什么?

回答by Santosh

There is no need for any if condition. We can get the last used row of column C and fill the data in column D accordingly.

不需要任何 if 条件。我们可以得到 C 列最后使用的行,并相应地填充 D 列中的数据。

Sub test()

    Dim lastRow As Long
    lastRow = Range("C" & Rows.Count).End(xlUp).Row
    Range("D2").AutoFill Destination:=Range("D2:D" & lastRow)

End Sub