|
发表于 2022-11-27 11:12
来自手机
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
=A15=============
Attribute VB_Name = "模块1"
Sub 不打开合并多簿多表且列标题相同叠加在下()
Dim mp, mn, aw, WbN, wn
Dim wb As Workbook, Num
Dim ge, e, endRow, endColumn
Dim arr, i&, j&, k&, dic As Object, iRow&, iCol&, vItem
Dim sht As Worksheet, brr
Set dic = CreateObject("Scripting.Dictionary")
' Dim brr(1 To 1048576, 1 To 100)
Application.ScreenUpdating = False
mp = ActiveWorkbook.Path
mn = Dir(mp & "\" & "*.xls?")
aw = ActiveWorkbook.Name
Num = 0
Do While mn <> ""
If mn <> aw Then
Set wb = Workbooks.Open(mp & "\" & mn)
ge = ge + 1
With Workbooks(1).ActiveSheet
For i = 1 To Sheets.Count
wb.Sheets(i).AutoFilterMode = False
endColumn = wb.Sheets(i).UsedRange.Columns.Count
endRow = wb.Sheets(i).UsedRange.Rows.Count '- 1
Next
ReDim brr(1 To 1048576, 1 To 100)
iCol = 1: iRow = 1: brr(1, 1) = "数据源"
'' 下面代码判定第一行的列标是否相同
For i = 1 To Worksheets.Count
' arr = wb.Sheets(i).Range(wb.Sheets(i).Cells(1, 1), wb.Sheets(i).Cells(endRow, endColumn))
arr = wb.Sheets(i).UsedRange
For j = 1 To UBound(arr, 2)
debug.print j
debug.print arr(1,j)
stop
rem 在本地窗口看看是否 arr没有设置
If Not dic.exists(arr(1, j)) Then '这句代码通不过,提示如表里截图错别???
iCol = iCol + 1
brr(1, iCol) = arr(1, j)
dic(arr(1, j)) = iCol
End If
Next j
Next i
'' 下面代码合并数据且如果列标相同则往下叠加
For i = 1 To Sheets.Count
' arr = wb.Sheets(i).Range(wb.Sheets(i).Cells(1, 1), wb.Sheets(i).Cells(endRow, endColumn))
arr = wb.Sheets(i).UsedRange
For j = 2 To UBound(arr)
iRow = iRow + 1
brr(iRow, 1) = wb.Name & wb.Sheets(i).Name
For k = 1 To UBound(arr, 2)
vItem = dic(arr(1, k))
brr(iRow, vItem) = arr(j, k)
Next k
Next j
Next i
'' 将合并数据赋给当前工作表
Set dic = Nothing
Cells.Clear
[a1].Resize(iRow, iCol) = brr
' With [a1].CurrentRegion
' .HorizontalAlignment = xlCenter
' .Borders.LineStyle = xlContinuous
' .Rows(1).Font.Bold = True
' .EntireColumn.AutoFit
' .EntireRow.AutoFit
' End With
WbN = WbN & Chr(13) & wb.Name
wb.Close False
End With
End If
mn = Dir
Loop
Range("a1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & ge & "个工作薄下全部工作表。如下:" & vbCrLf & WbN, vbInformation, "提示"
End Sub
|
评分
-
1
查看全部评分
-
|