請測試看看,謝謝
Sub test()
Dim Arr, xD, Brr(1 To 1000, 1 To 2), i&, n%, sh%
Set xD = CreateObject("Scripting.Dictionary")
For sh = 2 To Sheets.Count
With Sheets(sh)
Arr = .[a1].CurrentRegion
For i = 2 To UBound(Arr)
If xD.Exists(Arr(i, 1)) Then
If Not xD.Exists(Arr(i, 1) & "|" & Sheets(sh).Name) Then
n = n + 1: Brr(n, 1) = Arr(i, 1)
Brr(n, 2) = Sheets(sh).Name
End If
xD(Arr(i, 1) & "|" & Sheets(sh).Name) = ""
Else
xD(Arr(i, 1)) = ""
End If
Next
End With
xD.RemoveAll
Next
If n > 0 Then
With Sheets("總表")
.[a1].CurrentRegion.Offset(1) = ""
.Range("a2").Resize(n, 2) = Brr
End With
End If
End Sub作者: av8d 時間: 2022-1-8 16:07
Sub 巨集1()
Dim Arr, Brr(1 To 1000, 1), xD, S As Worksheet, SN$, i&, T$, N&
Set xD = CreateObject("Scripting.Dictionary")
For Each S In Sheets
Arr = S.[a1].CurrentRegion: SN = S.Name
If Not SN Like "*#日" Then GoTo x01
For i = 2 To UBound(Arr)
T = Arr(i, 1): xD(T) = xD(T) + 1
If xD(T) = 2 Then N = N + 1: Brr(N, 0) = T: Brr(N, 1) = SN: xD(T) = -9 ^ 9
Next i
xD.RemoveAll
x01: Next
With Sheets("總表")
.[a1].CurrentRegion.Offset(1).ClearContents
If N > 0 Then .[a2].Resize(N, 2) = Brr
End With
End Sub作者: av8d 時間: 2022-1-9 14:15
Sub 巨集3()
Dim Arr, Brr(1 To 1000, 1 To 4), xD, S As Worksheet, SN$, i&, j%, T$, N&
Set xD = CreateObject("Scripting.Dictionary")
For Each S In Sheets
Arr = S.[a1].CurrentRegion: SN = S.Name
If Not SN Like "*#日" Then GoTo x01
For i = 2 To UBound(Arr)
T = Arr(i, 2): xD(T) = xD(T) + 1
If xD(T) = 2 Then
N = N + 1: xD(T) = -9 ^ 9
For j = 1 To 3: Brr(N, j) = Arr(i, j): Next
Brr(N, 4) = SN
End If
Next i
xD.RemoveAll
x01: Next
With Sheets("總表")
.[a1].CurrentRegion.Offset(1).ClearContents
If N > 0 Then .[a2].Resize(N, 4) = Brr
End With
End Sub作者: av8d 時間: 2022-1-11 13:00
回復 15#av8d
請測試看看,謝謝
Sub test()
Dim Arr, xD, Brr(1 To 1000, 1 To 4), i&, n%, sh%, j%, T$
Set xD = CreateObject("Scripting.Dictionary")
For sh = 2 To Sheets.Count
With Sheets(sh)
Arr = .[a1].CurrentRegion
For i = 2 To UBound(Arr)
T = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3): xD(T) = xD(T) + 1
Next
End With
Next
For sh = 2 To Sheets.Count
With Sheets(sh)
Arr = .[a1].CurrentRegion
For i = 2 To UBound(Arr)
T = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3)
If xD(T) > 1 Then
n = n + 1: For j = 1 To 3: Brr(n, j) = Arr(i, j): Next
Brr(n, 4) = Sheets(sh).Name
End If
Next
End With
Next
If n > 0 Then
With Sheets("總表")
.[a1].CurrentRegion.Offset(1) = ""
.Range("a2").Resize(n, 4) = Brr
End With
End If
End Sub作者: 准提部林 時間: 2022-1-13 16:36
以下兩種代碼僅供參考, 難以理解, 不想多做解釋了!
Sub TEST_A1()
Dim Arr, Brr, Crr, xD, S As Worksheet, SN$, T$, U&, i&, j%, k&, N&, P%
Set xD = CreateObject("Scripting.Dictionary")
ReDim Brr(1 To 30000, 1 To 4): Crr = Brr
For Each S In Sheets
Arr = S.[a1].CurrentRegion: SN = S.Name
If Not SN Like "*#日" Then GoTo x01
For i = 2 To UBound(Arr)
T = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3)
k = k + 1: Crr(k, 4) = SN: U = xD(T): P = 0
If U > 0 Then N = N + 1: P = 1
For j = 1 To 4
If j < 4 Then Crr(k, j) = Arr(i, j)
If U > 0 Then Brr(N, j) = Crr(U, j): xD(T) = -1
If U < 0 Or P = 1 Then Brr(N + 1, j) = Crr(k, j)
Next j
If xD(T) = 0 Then xD(T) = k Else N = N + 1
i01: Next i
x01: Next
With Sheets("總表")
.[a1].CurrentRegion.Offset(1).ClearContents
If N > 0 Then .[a2].Resize(N, 4) = Brr
End With
End Sub
'============================
Sub TEST_A2()
Dim Arr, Brr, Crr, xD, S As Worksheet, SN$, T$, i&, j%, k&, N&
Set xD = CreateObject("Scripting.Dictionary")
ReDim Brr(1 To 30000, 1 To 4): Crr = Brr
For Each S In Sheets
Arr = S.[a1].CurrentRegion: SN = S.Name
If Not SN Like "*#日" Then GoTo x01
For i = 2 To UBound(Arr)
k = k + 1: Crr(k, 4) = SN: T = "": U = 0
For j = 1 To 3
T = T & "|" & Arr(i, j)
Crr(k, j) = Arr(i, j)
Next j
If xD(T) = 0 Then xD(T) = k: GoTo i01
If xD(T) > 0 Then U = xD(T): N = N + 1: xD(T) = -1
For j = 1 To 4
If U > 0 Then Brr(N, j) = Crr(U, j)
Brr(N + 1, j) = Crr(k, j)
Next
N = N + 1
i01: Next i
x01: Next
With Sheets("總表")
.[a1].CurrentRegion.Offset(1).ClearContents
If N > 0 Then .[a2].Resize(N, 4) = Brr
End With
End Sub
兩個迴圈, 第一次寫入字典, 第二次讀取字典, 也可參考如下:
Sub TEST_A3()
Dim Arr, Brr(1 To 3000, 1 To 5), Crr, xD, T$, i&, j%, k&, N&, x%
Set xD = CreateObject("Scripting.Dictionary")
For x = 2 To Sheets.Count
Arr = Sheets(x).[a1].CurrentRegion
For i = 2 To UBound(Arr)
T = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3) '連接字串
xD(T) = xD(T) + 1 '使用字典累計出現次數
k = k + 1: Brr(k, 4) = Sheets(x).Name: Brr(k, 5) = T '暫時將各工作表內容(含工作表名稱及T連接文字)放入Brr
For j = 1 To 3: Brr(k, j) = Arr(i, j): Next
Next i
Next
For i = 1 To k '以Brr跑迴圈(此時不須再逐一讀取工作表, 同時減少文字連接動作, 加快速度)
If xD(Brr(i, 5)) > 1 Then N = N + 1 Else GoTo i01
For j = 1 To 4: Brr(N, j) = Brr(i, j): Next '重覆的由上而下再次寫入Brr
i01: Next i
With Sheets("總表")
.[a1].CurrentRegion.Offset(1).ClearContents
If N > 0 Then .[a2].Resize(N, 4) = Brr
End With
End Sub作者: av8d 時間: 2022-1-14 14:19