|
看看对不对
- Sub 指定合并()
- Dim wk As Workbook, sht As Worksheet, dcnt As Integer
- Dim brr(), d As Object, dic As Object
- Set d = CreateObject("scripting.dictionary")
- arr = wkname(ThisWorkbook.Path & "")
- ReDim brr(1 To UBound(arr))
- For i = 1 To UBound(arr)
- Set wk = Workbooks.Open(arr(i, 2))
- Set sht = wk.Sheets(1)
- brr(i) = sht.UsedRange
- brr(i)(1, 1) = wk.Name
- brr(i)(1, 2) = sht.Name
- wk.Close
- Next i
- With Sheets("1-2指定列字段合并")
- For j = 3 To 100
- If .Cells(3, j) = "" Then Exit For
- n = n + 1
- d(.Cells(3, j).Value) = n
- Next j
- ReDim crr(1 To UBound(brr))
- For r = 1 To UBound(brr)
- For i = 1 To UBound(brr(r))
- If InStr(brr(r)(i, 1), "年度") Then
- For j = 1 To UBound(brr(r), 2)
- If Trim(brr(r)(i + 1, j)) = vbNullString Then Exit For
- If d.exists(brr(r)(i + 1, j)) Then
- crr(r) = bi(d, brr(r), i + 1, d.Count + 2)
- Exit For
- End If
- Next j
- End If
- Next i
- Next r
- ReDim frr(1 To 10000, 1 To d.Count + 2)
- For r = 1 To UBound(crr)
- For i = 1 To UBound(crr(r))
- If crr(r)(i, 6) = "" Then Exit For
- k = k + 1
- For j = 1 To UBound(crr(r), 2)
- frr(k, j) = crr(r)(i, j)
- Next j
- Next i
- Next r
- .Range("a4").Resize(k, j) = frr
- End With
- End Sub
- Sub 全部合并()
- Dim wk As Workbook, sht As Worksheet, dcnt As Integer
- Dim brr(), d As Object, dic As Object
- Set d = CreateObject("scripting.dictionary")
- arr = wkname(ThisWorkbook.Path & "")
- ReDim brr(1 To UBound(arr))
- For i = 1 To UBound(arr)
- Set wk = Workbooks.Open(arr(i, 2))
- Set sht = wk.Sheets(1)
- brr(i) = sht.UsedRange
- brr(i)(1, 1) = wk.Name
- brr(i)(1, 2) = sht.Name
- wk.Close
- Next i
- n = 2
- For r = 1 To UBound(brr)
- For i = 1 To UBound(brr(r))
- If InStr(brr(r)(i, 1), "年度") Then
- brr(r)(1, 3) = i + 1
- For j = 1 To UBound(brr(r), 2)
- If Trim(brr(r)(i + 1, j)) = vbNullString Then
- brr(r)(1, 4) = j - 1
- Exit For
- End If
- If Not d.exists(brr(r)(i + 1, j)) Then
- n = n + 1
- d(brr(r)(i + 1, j)) = n
- End If
- Next j
- End If
- Next i
- Next r
- ReDim crr(1 To 10000, 1 To d.Count + 2)
- For r = 1 To UBound(brr)
- For i = brr(r)(1, 3) + 1 To UBound(brr(r))
- If brr(r)(i, 6) = "" Then Exit For
- k = k + 1
- crr(k, 1) = brr(r)(1, 1)
- crr(k, 2) = brr(r)(1, 2)
- For j = 1 To brr(r)(1, 4)
- If d.exists(brr(r)(brr(r)(1, 3), j)) Then
- s = d(brr(r)(brr(r)(1, 3), j))
- crr(k, s) = brr(r)(i, j)
- End If
- Next j
- Next i
- Next r
- With Sheets("1-1全部列字段合并")
- .UsedRange = ""
- .Range("a1") = "工作簿名"
- .Range("b1") = "工作表名"
- .Range("c1").Resize(1, d.Count) = d.keys
- .Range("a2").Resize(k, d.Count) = crr
- .Cells.Columns.AutoFit
- End With
- End Sub
- Function wkname(mpath As String) As Variant
- Dim arr()
- sh = Dir(mpath & "*.xlsx")
- k = 1
- Do While sh <> ""
- If sh <> ThisWorkbook.Name Then
- ReDim Preserve arr(1 To 2, 1 To k)
- arr(1, k) = k
- arr(2, k) = mpath & sh
- k = k + 1
- End If
- sh = Dir
- Loop
- wkname = Application.Transpose(arr)
- End Function
- Function bi(dic As Object, mbrr As Variant, qs As Integer, dcnt As Integer) As Variant
- ReDim err(1 To 10000, 1 To dcnt)
- For i = qs + 1 To UBound(mbrr)
- If mbrr(qs, 1) = "" Then Exit For
- k = k + 1
- For j = 1 To UBound(mbrr, 2)
- If mbrr(qs, j) = "" Then Exit For
- If dic.exists(mbrr(qs, j)) Then
- err(k, 1) = mbrr(1, 1)
- err(k, 2) = mbrr(1, 2)
- err(k, dic(mbrr(qs, j)) + 2) = mbrr(i, j)
- End If
- Next j
- Next i
- bi = err
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|