For Each a In Range("C5", [O65536].End(xlUp)).SpecialCells(xlCellTypeConstants) '在C:O欄的的非空格做迴圈
If a <> 0 Then
If IsEmpty(d(a.Value)) Then d(a.Value) = Cells(a.Row, 2) Else d(a.Value) = d(a.Value) & "," & Cells(a.Row, 2) '如果未出現過就以該儲存格內容加入否則就將原有字串以逗點連結該儲存格內容
End If
Next
For Each a In Range("q5", [q65536].End(xlUp)) '在K欄所有資料做迴圈
ar = Split(d(a.Value), ",") '將以K欄儲存格為關鍵字的字典內容以逗點做分割得到一個陣列
a.Offset(, 1).Resize(, UBound(ar) + 1) = ar '將陣列寫入L欄向右
Next
End Sub
複製代碼
作者: Andy2483 時間: 2023-4-12 15:22
謝謝論壇,謝謝各位前輩
後學藉此帖練習陣列與字典,學習方案如下,請各位前輩指教
執行前:
[attach]36121[/attach]
執行結果:
[attach]36122[/attach]
Option Explicit
Sub TEST()
Dim Brr, Y, i&, j&, N&, T$, T1$, Ma%
Dim xR As Range, Ra As Range, Sh As Worksheet
Set Y = CreateObject("Scripting.Dictionary")
Set Sh = ActiveSheet: [Q:IA].ClearContents
Set xR = Range(Sh.[IA5], Sh.Cells(Rows.Count, "B").End(3))
Brr = xR
For i = 1 To UBound(Brr)
T1 = Brr(i, 1): If T1 = "" Then GoTo i01
For j = 2 To 14
T = Brr(i, j): If T = "" Then GoTo i02
If InStr(T, "A") Then
T = Format(Mid(T, 2), "A" & "000")
If Y(T) = "" Then
N = N + 1: Brr(N, 16) = T
Y(T) = N: Y(T & "/c") = 17
Else
Y(T & "/c") = Y(T & "/c") + 1
If Ma < Y(T & "/c") Then Ma = Y(T & "/c")
End If
Brr(Y(T), Y(T & "/c")) = T1
End If
i02:
Next
i01:
Next
With [B5].Resize(UBound(Brr), Ma + 16)
.Value = Brr
With Intersect(.Cells, .Cells.Offset(0, 15))
.Sort KEY1:=.Item(1), Order1:=1, Header:=2
For i = 1 To N: .Item(i, 1) = "A" & Val(Mid(.Item(i, 1), 2)): Next
End With
End With
Set Y = Nothing: Set xR = Nothing: Set Sh = Nothing: Erase Brr
End Sub