如何合并多个excel文件到一个excel文件中且要放在不同的工作表内
3个回答
展开全部
这个需要用宏来完成了,我可以给你一段代码
Sub 汇总()
Dim sr$, wb As Workbook, ws As Worksheet, rngs As Range
Dim cell As Range, rr As Range
sr = Dir(ThisWorkbook.Path & "\*考勤表*.xlsm")
Do
Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & sr)
For Each ws In wb.Worksheets
If ws.Name = "统计用表" Then
'Set rngs = ws.Range("B7:Y22").Select
Set rngs = ws.Range("a7", ws.[x7].End(xlDown))
'录制的内容为了去掉a列中的空格
Range("A7:X162").Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A7").Select
Set cell = ThisWorkbook.Worksheets(1).Cells(Rows.Count, 1).End(xlUp)(2, 1)
cell.Resize(rngs.Rows.Count, rngs.Columns.Count) = rngs.Value
cell(1, 25).Resize(rngs.Rows.Count, 1) = Left(wb.Name, Len(wb.Name) - 5)
Set rr = Range(ThisWorkbook.Worksheets(1).[a7].End(xlDown)(2, 1), ThisWorkbook.Worksheets(1).[y7].End(xlDown)(2, 1))
rr.ClearContents
'Len(cell.Value) = 2 Or Len(cell.Value)
End If
Next
wb.Close
sr = Dir
Loop Until sr = ""
End Sub
建立一个启用宏的工作簿,放在要汇总的文件夹下没,注意名字不能和其他表有重复,考勤表那三个字 换成你要汇总的表里共有的名字,中间有个统计用表是要被汇总的工作簿中的某个工作表的名称,要汇总哪个就写哪个的名字,倒数第9行 worksheets(1)就是指放在第一个工作表,后面就是你自己要改的了
还有'Set rngs = ws.Range("B7:Y22").Select这个是要复制的区域
哎u有我日 这个好麻烦的说 还有一些跟实际情况 相关参数要改,你要是能看懂一点就能自己改了
Sub 汇总()
Dim sr$, wb As Workbook, ws As Worksheet, rngs As Range
Dim cell As Range, rr As Range
sr = Dir(ThisWorkbook.Path & "\*考勤表*.xlsm")
Do
Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & sr)
For Each ws In wb.Worksheets
If ws.Name = "统计用表" Then
'Set rngs = ws.Range("B7:Y22").Select
Set rngs = ws.Range("a7", ws.[x7].End(xlDown))
'录制的内容为了去掉a列中的空格
Range("A7:X162").Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A7").Select
Set cell = ThisWorkbook.Worksheets(1).Cells(Rows.Count, 1).End(xlUp)(2, 1)
cell.Resize(rngs.Rows.Count, rngs.Columns.Count) = rngs.Value
cell(1, 25).Resize(rngs.Rows.Count, 1) = Left(wb.Name, Len(wb.Name) - 5)
Set rr = Range(ThisWorkbook.Worksheets(1).[a7].End(xlDown)(2, 1), ThisWorkbook.Worksheets(1).[y7].End(xlDown)(2, 1))
rr.ClearContents
'Len(cell.Value) = 2 Or Len(cell.Value)
End If
Next
wb.Close
sr = Dir
Loop Until sr = ""
End Sub
建立一个启用宏的工作簿,放在要汇总的文件夹下没,注意名字不能和其他表有重复,考勤表那三个字 换成你要汇总的表里共有的名字,中间有个统计用表是要被汇总的工作簿中的某个工作表的名称,要汇总哪个就写哪个的名字,倒数第9行 worksheets(1)就是指放在第一个工作表,后面就是你自己要改的了
还有'Set rngs = ws.Range("B7:Y22").Select这个是要复制的区域
哎u有我日 这个好麻烦的说 还有一些跟实际情况 相关参数要改,你要是能看懂一点就能自己改了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询