Sub BatchSum()
Dim FilePath As String
Dim FileName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Integer
' 设置文件夹的路径
FilePath = "C:\Your\Folder\Path\"
' 查找和循环打开该路径下每个扩展名为xls或xlsx的文件
FileName = Dir(FilePath & "*.xls*")
While FileName ""
Set wb = Workbooks.Open(FilePath & FileName)
For i = 1 To wb.Sheets.Count
Set ws = wb.Sheets(i)
With ws
' 计算F列和G列的和,并将结果放在F列和G列的最下方单元格
.Range("F" & .Rows.Count).End(xlUp).Offset(1, 0).Formula = "=SUM(F:F)"
.Range("G" & .Rows.Count).End(xlUp).Offset(1, 0).Formula = "=SUM(G:G)"
End With
Next i
wb.Save
wb.Close
FileName = Dir
Wend
End Sub
将上述代码复制到一个新的 VB 模块中,并将其中的 FilePath 设置为要处理的文件夹的路径,然后运行该宏文件即可批量对指定文件夹内的 Excel 文件求和并保存。