|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
可以汇总所有工作簿的所有工作表,汇集表中可以没有标题
Sub 按字段汇总工作簿()
Dim i&, j&, k&, m&
Dim arData As Variant
Dim wb As Workbook
Dim d As Object
Dim c As New Collection
Set d = CreateObject("Scripting.Dictionary")
With ThisWorkbook.Sheets("汇集表")
For j = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column
If Not d.exists(.Cells(1, j).Value) And .Cells(1, j).Value <> "" Then
d.Add .Cells(1, j).Value, j
End If
Next j
End With
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Add "Excel files", "*.xlsx; *.xls", 1
.AllowMultiSelect = True
If .Show = -1 Then
For k = 1 To .SelectedItems.Count
With GetObject(.SelectedItems(k))
For i = 1 To .Worksheets.Count
c.Add .Worksheets(i).[a1].CurrentRegion.Value
Next i
End With
Next k
Else
MsgBox "取消了文件选择!": Exit Sub
End If
End With
For k = 1 To c.Count
m = m + UBound(c(k))
For j = 1 To UBound(c(k), 2)
If Not d.exists(c(k)(1, j)) Then d.Add c(k)(1, j), d.Count + 1
Next j
Next k
ReDim arData(1 To m, 1 To d.Count)
For j = 1 To d.Count
arData(1, j) = d.keys()(j - 1)
Next j
m = 1
For k = 1 To c.Count
For i = 2 To UBound(c(k))
m = m + 1
For j = 1 To UBound(c(k), 2)
arData(m, d(c(k)(1, j))) = c(k)(i, j)
Next j
Next i
Next k
With ThisWorkbook.Sheets("汇集表")
.Cells.ClearContents
.[a1].Resize(UBound(arData), UBound(arData, 2)) = arData
End With
End Sub
|
评分
-
1
查看全部评分
-
|