用公式VBA填充单元格直到最后一行

时间:2018-06-19 19:18:25

标签: vba

ExcelSheetImage 我想填充单元格,直到最后一行和最后一列。对于我要浏览的每个Excel工作表,列和行的数量都不同。

到目前为止,我下面的代码用于填充C列中的所有行,但无法编译

Sub populate()
Dim lastrow, lastcol As Long

lastrow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row + 1 'finds last row in B
Range(lastrow, 3).Formula = "=sum(A2:B2)"  'populates cells in C with formula 
until last row
End Sub 

1 个答案:

答案 0 :(得分:0)

Range需要两个单元格的开始和结束或一个字符串。 Cells行和列

Sub populate()
Dim lastrow, lastcol As Long

lastrow = Sheet1.Cells(Sheet1.Rows.Count, 2).End(xlUp).Row + 1 'finds last row in B
Sheet1.Range("C2",Sheet1.Cells(lastrow, 3).Formula = "=sum(A2:B2)"  'populates cells in C with formula 
End Sub