|
楼主 |
发表于 2017-6-18 22:12
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
哦,你好,帮我看,能不能改后,能得到我想要的
- Sub text()
- Dim ar1, ar2, di1 As Object, di2 As Object
- Dim i, j, k, a, b, c, ar
- Dim m, n, arr()
- With Sheets("代码没有执行前")
- ar1 = .Range("a1").Resize(.Cells(.Rows.Count, 1).End(3).Row, 7)
- ar2 = .Range("h1").Resize(.Cells(.Rows.Count, 8).End(3).Row, 5)
- End With
- Set di1 = CreateObject("scripting.dictionary")
- Set di2 = CreateObject("scripting.dictionary")
- For i = 2 To UBound(ar1)
- If ar1(i, 1) <> "" Then
- di1(ar1(i, 1)) = di1(ar1(i, 1)) & vbTab & i
- di2(ar1(i, 1)) = ""
- End If
- Next
- For i = 2 To UBound(ar2)
- If ar2(i, 1) <> "" Then
- If Not di1.exists(ar2(i, 1)) Then di1.Add ar2(i, 1), ""
- di2(ar2(i, 1)) = di2(ar2(i, 1)) & vbTab & i
- End If
- Next
- ar = di1.keys
- ReDim arr(1 To 12, 1 To 1)
- For j = 1 To UBound(ar1, 2)
- arr(j, 1) = ar1(1, j)
- Next
- For j = 1 To UBound(ar2, 2)
- arr(j + UBound(ar1, 2), 1) = ar2(1, j)
- Next
- For j = 0 To UBound(ar)
- b = UBound(arr, 2)
- m = Split(di1(ar(j)), vbTab): n = Split(di2(ar(j)), vbTab)
- a = IIf(UBound(m) >= UBound(n), UBound(m), UBound(n))
- c = b + a
- ReDim Preserve arr(1 To 12, 1 To c)
- If UBound(m) > 0 Then
- For i = 1 To UBound(m)
- For k = 1 To UBound(ar1, 2)
- arr(k, b + i) = ar1(m(i), k)
- Next
- Next
- End If
- If UBound(n) > 0 Then
- For i = 1 To UBound(n)
- For k = 1 To UBound(ar2, 2)
- arr(k + UBound(ar1, 2), b + i) = ar2(n(i), k)
- Next
- Next
- End If
- Next
- With Sheets("代码执行后的结果")
- .Range("a1").Resize(.Rows.Count, 12).Clear
- .Columns("b:b").NumberFormatLocal = "@"
- .Columns("i:i").NumberFormatLocal = "@"
- .Columns("c:c").NumberFormatLocal = "@"
- .Range("a1").Resize(UBound(arr, 2), 12) = WorksheetFunction.Transpose(arr)
- End With
- Set di1 = Nothing: Set di2 = Nothing: Erase ar1, ar2, ar, m, n, arr
- End Sub
复制代码 |
|