ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1528|回复: 3

[求助] 同一文件夹多工簿相同工作表合并代码 一直不成功 找不到问题所在,求大侠指点迷津...

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-9-5 20:42 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 合并当前目录下所有工作簿的全部工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long
Dim Num As Long
Dim BOX As String
Dim r, c
Application.ScreenUpdating = False
MyPath = "C:\Users\Administrator\Desktop\try"
MyName = Dir(MyPath & "\" & "*.xlsx")
AWbName = ActiveWorkbook.Name
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
With Workbooks(1).ActiveSheet
.Cells(.Range("B65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
For G = 1 To Sheets.Count
Wb.Sheet(G).Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
r = Selection.Rows.Count + 2
Wb.Sheet(G).Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
c = Selection.Columns.Count
Wb.Sheet(G).Range("a3", Cells(r, c)).Copy.ActiveSheet.Cells(.Range("A65536").End(xlUp).Row + 1,1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("a2").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub


TA的精华主题

TA的得分主题

发表于 2018-9-5 21:09 | 显示全部楼层
建议上传附件来测试查看

TA的精华主题

TA的得分主题

发表于 2018-9-5 21:18 | 显示全部楼层
大体看了一下
  1  路径搞得太复杂
  2  Workbooks.Open(MyPath & "\" & MyName) 这个是打开目标工作簿   你上面定义了AWbName = ActiveWorkbook.Name  因此就存在两个激活的工作簿  程序当然无法识别   下面的没细看
  

TA的精华主题

TA的得分主题

发表于 2018-9-5 21:21 | 显示全部楼层
Sub 合并当前目录下所有工作簿的全部工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long
Dim Num As Long
Dim BOX As String
Dim r, c
Application.ScreenUpdating = False
MyPath = "C:\Users\Administrator\Desktop\try"
MyName = Dir(MyPath & "\" & "*.xlsx")
AWbName = ActiveWorkbook.Name
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
With Workbooks(1).ActiveSheet
.Cells(.Range("B65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)

with Wb.Sheet(1)’1代表第一个工作表,可以修改为自己想提取数据的工作表名称,即Sheet("工作表")
.Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
r = Selection.Rows.Count + 2
.Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
c = Selection.Columns.Count
.Range("a3", Cells(r, c)).Copy.ActiveSheet.Cells(.Range("A65536").End(xlUp).Row + 1,1)
end with

WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("a2").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-13 15:41 , Processed in 0.021921 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表