|
- Option Explicit
- Sub test1() '
-
- Dim strPath As String, strFile As String
- Dim results, data, pos() As Long, dict As Object, target As Range
- Dim i As Long, j As Long, rowSize As Long, x As Long, y As Long
-
- Set dict = CreateObject("Scripting.Dictionary")
- dict.CompareMode = TextCompare '标题中有大写的 SKU 小写的 sku 这里不区分大小写
-
- rowSize = 1
- With Range("A1").CurrentRegion
- .Offset(rowSize).Clear 'Contents
- data = .Rows(rowSize).Value
- results = .Resize(50000)
- End With
-
- DoApp False
-
- For j = 1 To UBound(data, 2)
- If Len(data(rowSize, j)) Then dict.Add data(rowSize, j), j
- Next
-
- strPath = ThisWorkbook.Path & "\"
- strFile = Dir(strPath & "*.xls*")
- While Len(strFile)
- If strPath & strFile <> ThisWorkbook.FullName Then
- With Workbooks.Open(strPath & strFile, 0)
- With .Worksheets(1)
- Set target = .Cells.Find("时间", , xlValues, , xlByRows, xlPrevious)
- If Not target Is Nothing Then
- y = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
- x = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
- With .Range("A1", .Cells(y, x))
- data = Intersect(.Offset(0), .Offset(target.Row - 1)).Value
- x = 0
- For j = 1 To UBound(data, 2)
- If Len(data(1, j)) Then
- If dict.Exists(data(1, j)) Then
- x = x + 1
- ReDim Preserve pos(1, 1 To x)
- pos(0, x) = j
- pos(1, x) = dict(data(1, j))
- End If
- End If
- Next
- If x Then
- For i = 2 To UBound(data)
- rowSize = rowSize + 1
- For j = 1 To UBound(results, 2) '本是 1 to x ,但 03.xlsx 文件中 SKU 重复了,最后一个舍弃不汇总
- results(rowSize, pos(1, j)) = data(i, pos(0, j))
- Next
- Next
- End If
- End With
- End If
- End With
- .Close False
- End With
- End If
- strFile = Dir
- Wend
-
- With Range("A1").Resize(rowSize, UBound(results, 2))
- .Borders.Weight = xlHairline
- .HorizontalAlignment = xlCenter
- .Value = results
- End With
-
- Set target = Nothing
- Set dict = Nothing
- DoApp
- Beep
- End Sub
- Function DoApp(Optional b As Boolean = True)
- With Application
- .ScreenUpdating = b
- .DisplayAlerts = b
- .Calculation = -b * 30 - 4135
- End With
- End Function
复制代码 |
|