|
楼主 |
发表于 2022-9-16 11:06
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 合并多薄多表为一薄多表()
Application.ScreenUpdating = False
Dim ww As Workbook
Dim sh As Worksheet
Dim arr(), brr()
Dim wb As Workbook
Dim dlgOpen As FileDialog
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set ww = ThisWorkbook
Set sht = ww.Worksheets("目录")
sht.[a1].CurrentRegion.Offset(1) = Empty
lj = ThisWorkbook.Path
VBA.ChDir lj
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
With dlgOpen
If .Show <> -1 Then MsgBox "您没有选择文件夹!": Exit Sub
lj = .SelectedItems(1)
End With
Application.DisplayAlerts = False
For Each sh In ww.Worksheets
If sh.Index > 1 Then sh.Delete
Next sh
Application.DisplayAlerts = True
t = Timer
f = Dir(lj & "\*.xls*")
m = 1
bt = InputBox("请输入待合并工作表的标题行数", "标题行数", "2")
If bt = "" Then MsgBox "您没有输入标题行数,请按确定退出!": Exit Sub
Do While f <> ""
If f <> ThisWorkbook.Name Then
Set wb = Workbooks.Open(lj & "\" & f, 0)
For Each sh In wb.Worksheets
r = sh.UsedRange.Rows.Count
If r > Val(bt) Then
If Not d.exists(sh.Name) Then
sh.Copy after:=ww.Worksheets(ww.Worksheets.Count)
d(sh.Name) = ""
m = m + 1
sht.Cells(m, 1) = m - 1
sht.Cells(m, 2) = mc
sht.Hyperlinks.Add anchor:=sht.Cells(m, 2), Address:="", SubAddress:="'" & sh.Name & "'!a1", TextToDisplay:=sh.Name
Else
rs = ww.Worksheets(sh.Name).UsedRange.Rows.Count + 1
sh.Rows(Val(bt) + 1 & ":" & r).Copy ww.Worksheets(sh.Name).Cells(rs, 1)
End If
End If
Next sh
wb.Close False
End If
f = Dir
Loop
Application.ScreenUpdating = True
MsgBox "合并完毕!耗时:" & Format(Timer - t, "0.00") & "秒", 64, "EXCEL提醒"
End Sub
|
评分
-
3
查看全部评分
-
|