|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test0()
-
- Dim ar, br() As String, cr() As Long, dict As Object
- Dim i As Long, j As Long, k As Long, p As Long, s As String
-
- Application.ScreenUpdating = False
-
- Set dict = CreateObject("Scripting.Dictionary")
- ar = Range("I1:R" & Cells(Rows.Count, "K").End(xlUp).Row)
- ReDim br(1 To UBound(ar) - 1, 1 To UBound(ar, 2))
- For j = 1 To UBound(ar, 2)
- s = Trim(ar(1, j))
- If Len(s) Then dict.Add s, j
- Next
- For i = 2 To UBound(ar)
- s = Trim(ar(i, 3))
- If Len(s) Then dict.Add s, i - 1
- br(i - 1, 3) = s
- Next
-
- With Workbooks.Open(ThisWorkbook.Path & "\1.xlsm", 0)
- ar = .Worksheets("数据").Range("A1").CurrentRegion
- .Close False
- End With
-
- For j = 1 To UBound(ar, 2)
- s = Trim(ar(1, j))
- If dict.Exists(s) Then
- k = k + 1
- ReDim Preserve cr(1 To 2, 1 To k)
- cr(1, k) = j
- cr(2, k) = dict(s)
- End If
- Next
-
- For i = 2 To UBound(ar)
- s = Trim(ar(i, 1))
- If dict.Exists(s) Then
- p = dict(s)
- For j = 1 To k
- br(p, cr(2, j)) = ar(i, cr(1, j))
- Next
- End If
- Next
-
- Range("I2").Resize(UBound(br), UBound(br, 2)) = br
-
- Set dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
|