类似于sheet1工作簿1的B1和sheet1工作簿2的B1 + .......工作簿的工作簿50的B1 = sheet1 MacroWorkbook的B1。 3张,说50张工作簿。
我更喜欢打开文件位置,而不是从目录中选择。
在不同论坛的帮助下,我尝试从sheet1中获取多个工作簿中的总和:
Sub SUM_Workbooks()
Dim FileNameXls As Variant
Dim i As Integer
Dim wb As Workbook
FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)
If Not IsArray(FileNameXls) Then Exit Sub
Application.ScreenUpdating = False
For i = LBound(FileNameXls) To UBound(FileNameXls)
Set wb = Workbooks.Open(FileNameXls(i))
wb.Sheets(1).Range("A1:N1").Copy
ThisWorkbook.Sheets(1).Range("A1:N1").PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
wb.Close SaveChanges:=False
Next i
Application.ScreenUpdating = True
End Sub
我想将其扩展为3张。由于我不是VBA的专家,所以非常感谢帮助。谢谢!
答案 0 :(得分:1)
如果要对所有工作簿中的值(来自每个工作表A1:N1
)求和并将结果粘贴到此工作簿的A1:N1
中,请使用以下值:
Sub SUM_Workbooks()
Dim FileNameXls, f
Dim wb As Workbook, i As Integer
FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)
If Not IsArray(FileNameXls) Then Exit Sub
Application.ScreenUpdating = False
For Each f In FileNameXls
Set wb = Workbooks.Open(f)
For i = 1 To 3
wb.Worksheets(i).Range("A1:N1").Copy
ThisWorkbook.Sheets(i).Range("A1:N1").PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, SkipBlanks:=True, Transpose:=False
Next i
wb.Close SaveChanges:=False
Next f
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub