如何将不同工作薄中某工作表A1单元格数据都汇总在同一工作表中?
韩老师用合并几个个人简历工作薄来说明这个问题。
如下图,在一个文件中有四个个人简历工作薄:
每个工作薄中的数据如下图:
汇总的结果如下:
VBA实现
在同一文件夹下新建一工作薄,在VBA代码窗口中输入如下代码:
Sub 汇总()Application.ScreenUpdating = FalseDim irow&, Filename$, fn$Dim wb As Workbook, sht As WorksheetSheet1.UsedRange.ClearContents '清除当前区域Sheet1.Cells(1, 1).Value = "姓名"Sheet1.Cells(1, 2).Value = "性别"Sheet1.Cells(1, 3).Value = "年龄"Sheet1.Cells(1, 4).Value = "籍贯"irow = Sheets(1).Range("A1048576").End(xlUp).Row '当前区域最后一行Filename = Dir(ThisWorkbook.Path & "*.xlsx")Do While Filename <> "" If Filename <> ThisWorkbook.Name Then fn = ThisWorkbook.Path & "" & Filename Set wb = GetObject(fn) Set sht = wb.Worksheets(1) ThisWorkbook.Sheets(1).Cells(irow + 1, 1) = sht.Cells(2, 2).Value ThisWorkbook.Sheets(1).Cells(irow + 1, 2) = sht.Cells(2, 4).Value ThisWorkbook.Sheets(1).Cells(irow + 1, 3) = sht.Cells(2, 6).Value ThisWorkbook.Sheets(1).Cells(irow + 1, 4) = sht.Cells(3, 2).Value wb.Close False irow = irow + 1 End If Filename = DirLoopApplication.ScreenUpdating = TrueEnd Sub
如下图:
即可实现。