感謝回覆
因為資料B C D欄同時相同情況,屬於1筆資料
但全部資料只能存在1筆,所以我沒辦法這樣子處理
請問有其他方式嗎?作者: rouber590324 時間: 2016-10-24 14:20
如下為版內前輩之作品.試試
Sub ab()
Dim arr As Variant
Dim t As Range
Dim s As New Collection
Dim i, j As Long
Dim myRng As Range
j = [B65536].End(xlUp).Row
Set myRng = Range("B1:B" & j)
On Error Resume Next
For Each t In myRng
s.Add Item:=Range(t, t.Offset(0, 2)), key:=CStr(t)
Next
ReDim arr(1 To s.Count)
For i = 1 To s.Count
[f65536].End(xlUp).Offset(1, 0).Resize(, 3) = s(i).Value
Next
End Sub作者: linsurvey2005 時間: 2016-10-24 14:41
Option Explicit
Sub TEST_1()
Dim Brr, Y, i&, j&, N&, T$
'↑宣告變數
Set Y = CreateObject("Scripting.Dictionary")
'↑令Y變數是 字典
Brr = Range([D1], Cells(Rows.Count, "A").End(xlUp))
'↑令Brr變數是 二維陣列,以儲存格值帶入陣列裡
For i = 2 To UBound(Brr)
'↑設順迴圈
For j = 2 To 4: T = T & Brr(i, j) & "|": Next
'↑設內順迴圈!收集字串以"|"符號間隔
If Y(T) = "" Then
'↑如果以T變數查Y字典得item是 空字元?
N = N + 1: Y(T) = "@": T = ""
'↑令N變數累加1(指定結果資料列號),
'令T變數當key,item是 "@",納入Y字典(這是要註記item不是 "")
'令T變數是 空字元(因為下個迴圈執行前要清空此變數)
For j = 1 To 4: Brr(N + 1, j) = Brr(i, j): Next
'↑設內順迴圈!將符合條件的資料逐次帶入結果列
End If
Next
[H:K].ClearContents
'↑令清除舊結果資料
[H1].Resize(N + 1, 4) = Brr
'↑令Brr陣列資料從[H1]儲存格開始寫入值
'(N+1:是因為第一列是標題列,而N的累計不包含標題列)
Set Y = Nothing: Erase Brr
'↑令釋放變數
End Sub