|
我平时的工作需要经常执行加载宏,但遇到一个怪现象,代码如下,可以实现合并文件夹下所有工作簿的功能,但实际使用时发现用快捷键执行加载宏和进入加载宏执行居然效果不一样!进入加载宏执行可以完美实现效果,用快捷键执行加载宏居然不能成功!恳请大侠帮忙看看原因是什么,谢谢!
Sub 合并文件夹下所有工作簿()
'文件夹下所有工作簿wb所有工作表ws合并为一个新工作簿(但不含子文件夹),不修改数据
Dim write_wb As Workbook, wb As Workbook, sht As Worksheet, file_path$, file_name$
With Application.FileDialog(msoFileDialogFolderPicker)
'-------------------取得用户选择的文件夹路径---------------------------
If .Show Then file_path = .SelectedItems(1) Else Exit Sub
End With
If Right(file_path, 1) <> "\" Then file_path = file_path & "\"
file_name = Dir(file_path & "\*.xls")
Application.ScreenUpdating = False '关闭屏幕更新,加快程序运行
Application.DisplayAlerts = False '不显示警告信息
Set write_wb = Workbooks.Add '新建工作簿,合并文件
Do While file_name <> ""
Set wb = Workbooks.Open(file_path & file_name)
For Each sht In wb.Worksheets
sht.Copy After:=write_wb.Sheets(write_wb.Sheets.Count)
Next
wb.Close (False)
file_name = Dir '下一个文件名
Loop
'保存文件
save_file = file_path & "合并表.xlsx"
write_wb.SaveAs filename:=save_file
write_wb.Close (False)
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
测试用文件夹.rar
(185.65 KB, 下载次数: 10)
|
|