Excel 自动填充动态范围最后一行和最后一列

Excel 自动填充动态范围最后一行和最后一列,excel,excel-formula,autofill,vba,Excel,Excel Formula,Autofill,Vba,我有一本工作簿,里面有多张大小不一的纸。我想在最后一行之后添加一个total列,并跨所有列复制公式。我已经定义了最后一行和最后一列,公式显示在正确的位置,但我在尝试填充时收到一个错误。如何正确引用填充的两个动态单元格?我现在只使用一张纸进行测试,但最终会在书中的所有纸上循环 Sub Addtotals() Dim Bord As Worksheet Dim LRow As Long Dim LCol As Long Dim frmcell As Range

我有一本工作簿,里面有多张大小不一的纸。我想在最后一行之后添加一个total列,并跨所有列复制公式。我已经定义了最后一行和最后一列,公式显示在正确的位置,但我在尝试填充时收到一个错误。如何正确引用填充的两个动态单元格?我现在只使用一张纸进行测试,但最终会在书中的所有纸上循环

Sub Addtotals()

    Dim Bord As Worksheet
    Dim LRow As Long
    Dim LCol As Long
    Dim frmcell As Range

    Set Bord = Sheets("Borders")
    With Bord
    '--> Define last rows and columns
        LRow = .Range("A" & Rows.Count).End(xlUp).Row
        LCol = .Range("A" & Columns.Count).End(xlToLeft).Column

    '--> Add Total text to first column
        .Range("A" & LRow).Offset(1, 0).Select
        ActiveCell = "Total"

    '--> Add formula to next column
        Set frmcell = Range("B" & LRow + 1)
        frmcell.Formula = "=sum(B2:B" & LRow & ")"

    '--> Fill formula across range
        frmcell.Select
        Selection.AutoFill Destination:=Range(frmcell & LCol), Type:=xlFillDefault
    End With
End Sub
谢谢:)

像这样吗

Option Explicit

Sub Addtotals()
    Dim Bord As Worksheet
    Dim LRow As Long, LCol As Long

    Set Bord = Sheets("Borders")
    With Bord
    '--> Define last rows and columns
        LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
        LCol = .Cells(1, Columns.Count).End(xlToLeft).Column

    '--> Add Total text to first column
        .Range("A" & LRow).Value = "Total"

    '--> Fill formula across range
        .Range("B" & LRow & ":" & _
        Split(Cells(, LCol).Address, "$")(1) & LRow).Formula = _
        "=Sum(B2:B" & LRow - 1 & ")"
    End With
End Sub

伟大的谢谢。
split
的功能是什么?
split(Cells(,LCol).Address,“$”)(1)
从列号返回列名:)我想我会使用split选择最后一列进行复制并粘贴到新表上?是的:)前提是
LCol
包含最后一列的正确列号