回復 1#greetingsfromtw
請測試
Sub newtest()
Dim Arr, Brr(), myD As Object
Set myD = CreateObject("Scripting.Dictionary")
Arr = Range("a2:c" & Cells(Rows.Count, "a").End(3).Row).Value
For i = 1 To UBound(Arr)
For j = 0 To UBound(Split(Arr(i, 2), Chr(10)))
If Split(Arr(i, 2), Chr(10))(j) <> "" Then myD(Split(Arr(i, 2), Chr(10))(j)) = ""
Next j
For Each C In myD
n = n + 1
ReDim Preserve Brr(1 To 2, 1 To n)
Brr(1, n) = Arr(i, 1)
Brr(2, n) = C
Next C
myD.RemoveAll
Next i
[E2:F65536].ClearContents
[E2].Resize(n, 2) = Application.Transpose(Brr)
End Sub作者: greetingsfromtw 時間: 2016-11-16 12:07
Option Explicit
Sub TEST() '↑
Dim Brr, Crr, V, Y, R&, i&
'↑ 宣告變數
Set Y = CreateObject("Scripting.Dictionary")
'↑令Y變數是字典
Brr = Range([B2], Cells(Rows.Count, 1).End(xlUp))
'↑令Brr變數是 二維陣列,以儲存格值帶入陣列中
ReDim Crr(1 To 1000, 1 To 2)
'↑宣告 Crr變數是二維空陣列,縱向索引號1~1000,橫向索引號1~2
For i = 1 To UBound(Brr)
'↑設順迴圈!i從1到Brr縱向最大索引列號
For Each V In Split(Brr(i, 2) & vbLf, vbLf)
'↑設逐項迴圈!令V變數是以換行字元分割Brr陣列第2欄字串的一維陣列值之一
If Trim(V) = "" Then GoTo v01
'↑如果V變數去除頭尾空字元後是 空字元!就跳到v01標示位置繼續執行
If Y(Brr(i, 1) & "|" & V) <> "" Then GoTo v01
'↑如果Brr陣列第1欄字串連接"|",再連接V變數所組成的字串查Y字典item值,
'item值不是空字元!就跳到v01標示位置繼續執行
R = R + 1: Y(Brr(i, 1) & "|" & V) = 1
'↑令R變數累加1
'令在Y字典裡的(key:Brr陣列第1欄字串連接"|",再連接V變數所組成的字串),
'item="" 改為 1,
Crr(R, 1) = Brr(i, 1): Crr(R, 2) = V
'↑令R變數列第1欄Crr陣列是 i迴圈列第1欄Brr陣列值,
'↑令R變數列第2欄Crr陣列是 V變數
v01: Next
Next
[E2].Resize(R, 2) = Crr
'↑令[E2]擴展向下R變數列,向右擴展2欄範圍儲存格值以Crr陣列值帶入
Set Y = Nothing: Erase Brr, Crr
'↑令釋放變數
End Sub