更简单的方法excel方便的快捷键

原文

可以框选一个区域,然后自动输入求和公式,最右列可以求和框选行,最下行可以求和框选列。

Sub 插入求和公式()
    Dim selectedRange As Range
    Dim lastRow As Long, lastCol As Long
    Dim i As Long
 
    ' 获取用户选择的区域
    Set selectedRange = Selection
 
    ' 获取选择区域的最后一行和最后一列
    lastRow = selectedRange.Rows.Count
    lastCol = selectedRange.Columns.Count
 
    ' 在最右列插入求和公式,求和行
    For i = 1 To lastRow
        selectedRange.Cells(i, lastCol + 1).Formula = "=SUM(" & selectedRange.Rows(i).Address & ")"
    Next i
 
    ' 在最下行插入求和公式,求和列
    For i = 1 To lastCol
        selectedRange.Cells(lastRow + 1, i).Formula = "=SUM(" & selectedRange.Columns(i).Address & ")"
    Next i
 
    ' 最下行最右列插入总和公式
    selectedRange.Cells(lastRow + 1, lastCol + 1).Formula = "=SUM(" & selectedRange.Address & ")"
 
    ' 自动调整列宽
    selectedRange.Resize(lastRow + 1, lastCol + 1).Columns.AutoFit
End Sub