我试图将(A和B)之前的两个填充到c列的最后一行。 但是,我的代码只插入公式而不填写。如果我继续执行代码,它将填充一行。然后,如果我再次单击执行,它将填充另一行。
Sub row()
Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Select
ActiveCell.Formula = "=year(today())" 'this will be inserted into the first empty cell in column B
ActiveCell.Offset(0, -1).Value = "Actual" ''this will be inserted into the first empty cell in column A
ActiveCell.FillDown
end sub
答案 0 :(得分:2)
也许你的意思是这个?您需要阅读Filldown,因为您没有指定目的地范围。
Sub row()
With Range(Cells(1, 3), Cells(Rows.Count, 3).End(xlUp))
.Offset(, -1).Formula = "=year(today())"
.Offset(, -2).Value = "Actual"
End With
End Sub
答案 1 :(得分:0)
首先,从评论中获取Mat的Mug的建议,并确保您确定要调用Cells方法的工作表/工作簿。然后,尝试下面的代码。我相信FillDown只有在下面的单元格中有东西要替换时才会起作用。否则,如果填充空单元格,函数将不知道停止在哪里。相反,找到C列中最后使用过的单元格,然后立即在A行和B行的所有单元格中生成所需的值/函数。
Sub row()
Dim wb As Workbook
Dim ws as Worksheet
Dim rngA As Range
Dim rngB As Range
Dim rngC As Range
Set wb = ThisWorkbook
Set ws = wb.Worksheets("SheetNameHere") ' change this to an actual sheet name
Set rngA = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Set rngB = rngA.Offset(0,1)
Set rngC = ws.Cells(Rows.Count, 3).End(xlUp)
ws.Range(rngA,rngC.Offset(-2,0)).Value = "Actual" ''this will be inserted into ever empty cell in column A up to the last cell in Column C
ws.Range(rngB,rngC.Offset(-1,0)).Formula = "=year(today())" 'this will be inserted into every empty cell in column B up to the last cell in Column C
End Sub