[attach]31263[/attach][attach]31263[/attach] 字典的使用是在這論壇學到,真的很好用,這是第一次碰到字典導出報錯,KEY值沒有問題,但ITEM值報錯,也不知道原因出在那,只好求救了.

Sub A新DP判斷()
CL = Application.Match("C/L", [a1:a300], 0)  'C/L所在列數
CR2 = Application.Match("CUP PRINT", [a1:a100], 0)
D1 = Application.Match("D/P", [a1:a300], 0) + 2  'D/P資料區首列(不含標題列)
D2 = Cells(CR2, 1).End(3).Row 'D/P資料區末列
Ar = Range(Cells(D1, 1), Cells(D2, 9))
Set D = CreateObject("Scripting.Dictionary")
For x = 1 To UBound(Ar)  '將OS資料入陣列,item 則以"-"將OS串接
D(Ar(x, 3)) = D(Ar(x, 3)) & IIf(D(Ar(x, 3)) = "", "", "-") & Ar(x, 2)
Next x
If D.Count = 0 Then Exit Sub

ReDim Br(1 To D.Count, 1)
For Each a In D.keys
i = i + 1:  Br(i, 0) = a:  Br(i, 1) = D(a)
Next

Cells(D1, "J").Resize(D.Count, 2) = Br
End Sub

===================================

Sub A新DP判斷_2()
Dim Ar, Br, D, F1 As Range, F2 As Range, i&, N&, T\$, U&, V
[J:K].ClearContents
Set F1 = [A:A].Find("D/P", Lookat:=xlWhole)(3, 1) '尋找"D/P"的位置, 並向下移3格(即資料的開頭)
Set F2 = [A:A].Find("CUP PRINT").End(xlUp)(1, 3)  '尋找"CUP PRINT"的位置, 並向上取非空位置, 再向右移3格
Ar = Range(F1, F2)
ReDim Br(1 To UBound(Ar), 1 To 2)
Set D = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(Ar)
T = Ar(i, 3): If T = "" Then GoTo 99 '若AMOUNT為空, 略過
U = D(T) '取得AMOUNT已存入字典檔的位置
If U = 0 Then N = N + 1: U = N:  D(T) = N: Br(N, 1) = T '如果尚未存入字典檔, N+1遞增存入, 並將AMOUNT值放入Br陣列
Br(U, 2) = Replace(Trim(Br(U, 2) & " " & Ar(i, 2)), " ", "-") '依據U值的位置, 將B欄文字存入Br
99: Next i
If N > 0 Then Cells(F1.Row, "J").Resize(N, 2) = Br
End Sub

==============================

1. Option Explicit
2. Sub A新DP判斷()
3.     Dim CL As Integer, CR2 As Integer, D1 As Integer, D2 As Integer, D As Object
4.     Dim ar(), X As Integer, E As Variant
5.     CL = Application.Match("C/L", [a1:a300], 0)  'C/L所在列數
6.     CR2 = Application.Match("CUP PRINT", [a1:a100], 0)
7.     D1 = Application.Match("D/P", [a1:a300], 0) + 2  'D/P資料區首列(不含標題列)
8.     D2 = Cells(CR2, 1).End(3).Row 'D/P資料區末列
10.     ar = Range(Cells(D1, 1), Cells(D2, 9))
11.     Set D = CreateObject("Scripting.Dictionary")
12.     For X = 1 To UBound(ar)  '將OS資料入陣列,item 則以"-"將OS串接
13.         D(ar(X, 3)) = D(ar(X, 3)) & "+" & ar(X, 2)
14.     Next
15.     If D.Count = 0 Then Exit Sub
16.      '***改橫放不使用>>Application.Transpose
17.       Cells(D1, "J").Resize(, D.Count) = D.keys ''金額欄
18.       Cells(D1 + 1, "j").Resize(, D.Count) = D.ItemS ''OS欄
19.     '直放 **如陣列中元素的字元數有>255 元素 >> 需一一的導出
20.      X = 1
21.      For Each E In D.keys
22.         Cells(D1 + 3, "j").Range("A" & X) = E
23.         Cells(D1 + 3, "k").Range("A" & X) = D(E)
24.         X = X + 1
25.      Next
26.      '*** '***橫放 使用>>Application.Transpose*********************************
27.       Cells(D1 + 3, "L").Resize(D.Count, 1) = Application.Transpose(D.keys) '金額欄
28.        '陣列中元素的字元數有>255  '**使用Application.Transpose會發生錯誤
29.        ar = D.ItemS
30.       ar(0) = Mid(ar(0), 1, 256) 'OS欄 -->報錯 改為 ar(0) = Mid(ar(0), 1, 255)看看
31.       Cells(D1 + 3, "M").Resize(D.Count, 1) = Application.Transpose(ar) ''OS欄 -->報錯
32. End Sub

 歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)