vba 自动填充到列中的最后一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18972284/
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-11 23:28:23  来源:igfitidea点击:

Autofill to the last row in a column

excelvbaexcel-vba

提问by user2273278

Im trying to create a macro that edits the cell C2 to 0 and then autofills to the last row that column minus 1 (the row with j's). The code works for 3 sets of records but when there are more sets of data it doesn't autofill to the last row. For example If there are 4 records it will only autofill to the 3rd row and the 4th row will still have its old value. How do i make it so it fills to the last row when the number of records changes.

我试图创建一个宏,将单元格 C2 编辑为 0,然后自动填充到该列减去 1 的最后一行(带有 j 的行)。该代码适用于 3 组记录,但当有更多组数据时,它不会自动填充到最后一行。例如,如果有 4 条记录,它只会自动填充到第 3 行,第 4 行仍将具有其旧值。我如何使它在记录数发生变化时填充到最后一行。

Data:

数据:

ID  |  NAME  | CODE
1   |  JON   | 200
2   |  ANNA  | 300
3   |  TIM   | 400
jjj | jjjjjj | jjjjj

Code:

代码:

Dim codeCol as Long

range("C2").Select
ActiveCell.FormulaR1C1 = "0"
codeCol = Cells(1, Columns.Count).End(xlToLeft).Column
Selection.AutoFill Destination:=range("C2:H" & codeCol - 6), Type:=xlFillDefault

Output:

输出:

ID  |  NAME  | CODE
1   |  JON   | 0
2   |  ANNA  | 0
3   |  TIM   | 0
jjj | jjjjjj | jjjjj

回答by Sathish Kothandam

Tested

已测试

Try this

尝试这个

Sub test()
With ActiveSheet
 ' range("A" & rows.count).end(xlup).row)  - tells the position of last used row in A column

.Range("C2:C" & .Range("A" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "0"
End With
End Sub

Change the code as per your needs

根据您的需要更改代码