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

使用公式VBA填充单元格直到最后一行,vba,Vba,我想填充单元格,直到最后一行和最后一列。我正在浏览的每个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)" 'populat

我想填充单元格,直到最后一行和最后一列。我正在浏览的每个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 

Range
需要两个单元格,一个开始单元格,一个结束单元格或字符串<代码>单元格获取行和列

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