請測試看看,謝謝
Sub test()
Dim Arr, xD, Brr(), T$, T1$, i&, n%, m%
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([a1], [c65536].End(3))
ReDim Brr(1 To UBound(Arr), 1 To 3)
For i = 2 To UBound(Arr)
T = Arr(i, 2) & "|" & Arr(i, 3)
T1 = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3)
If xD.Exists(T) Then
m = xD(T)
If Not xD.Exists(T1) Then Brr(m, 3) = Brr(m, 3) + 1
Else
n = n + 1: xD(T) = n: xD(T1) = n
Brr(n, 1) = Arr(i, 2)
Brr(n, 2) = Arr(i, 3)
Brr(n, 3) = 1
End If
Next
With Range("g2").Resize(n, 3)
.Value = Brr
.Sort Key1:=.Item(1), Order1:=1, _
Key2:=.Item(2), Order2:=1, Header:=2
End With
End Sub作者: hcm19522 時間: 2021-11-19 15:10
Sub test()
Dim Arr, xD, Brr(), T$, T1$, i&, n%, m%
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([01!a1], [01!c65536].End(3))
ReDim Brr(1 To UBound(Arr), 1 To 3)
For i = 2 To UBound(Arr)
T = Arr(i, 2) & "|" & Arr(i, 3)
T1 = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3)
If xD.Exists(T) Then
m = xD(T)
If Not xD.Exists(T1) Then Brr(m, 3) = Brr(m, 3) + 1
Else
n = n + 1: xD(T) = n: xD(T1) = n
Brr(n, 1) = Arr(i, 2)
Brr(n, 2) = Arr(i, 3)
Brr(n, 3) = 1
End If
Next
With Range([02!g2]).Resize(n, 3)
.Value = Brr
.Sort Key1:=.Item(1), Order1:=1, _
Key2:=.Item(2), Order2:=1, Header:=2
End With
End Sub作者: ML089 時間: 2021-11-19 20:16
Sub test()
Dim Arr, xD, Brr(), T$, T1$, i&, n%, m%
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([01!a1], [01!c65536].End(3))
ReDim Brr(1 To UBound(Arr), 1 To 3)
For i = 2 To UBound(Arr)
T = Arr(i, 2) & "|" & Arr(i, 3)
T1 = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3)
If xD.Exists(T) Then
m = xD(T)
If Not xD.Exists(T1) Then Brr(m, 3) = Brr(m, 3) + 1
Else
n = n + 1: xD(T) = n: xD(T1) = n
Brr(n, 1) = Arr(i, 2)
Brr(n, 2) = Arr(i, 3)
Brr(n, 3) = 1
End If
Next
With Sheets("02").Range("g2").Resize(n, 3)
.Value = Brr
.Sort Key1:=.Item(1), Order1:=1, _
Key2:=.Item(2), Order2:=1, Header:=2
End With
End Sub作者: samwang 時間: 2021-11-20 07:51
2021/1/31 A123456 R001 ---> 這些我都設定同一日期(多列)
統計的數量又變成是出現"總"次數
>> 不好意思,更新如紅字,請測試看看,謝謝
Sub test2()
Dim Arr, xD, Brr(), T$, T1$, i&, n%, m%
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([01!a1], [01!c65536].End(3))
ReDim Brr(1 To UBound(Arr), 1 To 3)
For i = 2 To UBound(Arr)
T = Arr(i, 2) & "|" & Arr(i, 3)
T1 = Arr(i, 1) & "|" & Arr(i, 2) & "|" & Arr(i, 3)
If xD.Exists(T) Then
m = xD(T)
If Not xD.Exists(T1) Then Brr(m, 3) = Brr(m, 3) + 1: xD(T1) = n
Else
n = n + 1: xD(T) = n: xD(T1) = n
Brr(n, 1) = Arr(i, 2)
Brr(n, 2) = Arr(i, 3)
Brr(n, 3) = 1
End If
Next
With Sheets("02").Range("g2").Resize(n, 3)
.Value = Brr
.Sort Key1:=.Item(1), Order1:=1, _
Key2:=.Item(2), Order2:=1, Header:=2
End With
End Sub 作者: 准提部林 時間: 2021-11-20 18:03
Sub test_1()
Dim Arr, xD, Brr(), T$, T1$, i&, n%, m%
[02!g:i].ClearContents '不累計, 這要先清空
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([01!a1], [01!c65536].End(3))
ReDim Brr(1 To UBound(Arr), 1 To 3)
For i = 2 To UBound(Arr)
T = Arr(i, 2) & "|" & Arr(i, 3)
T1 = Arr(i, 1) & "|" & T
m = xD(T): xD(T1) = xD(T1) + 1
If m = 0 Then
n = n + 1: m = n: xD(T) = n
Brr(n, 1) = Arr(i, 2): Brr(n, 2) = Arr(i, 3)
End If
If xD(T1) = 1 Then Brr(m, 3) = Brr(m, 3) + 1
Next
[02!g1:i1] = [{"料號","批號","天數"}]
With [02!g2].Resize(n, 3)
.Value = Brr
.Sort Key1:=.Item(1), Order1:=1, _
Key2:=.Item(2), Order2:=1, Header:=2
End With
End Sub作者: jsc0518 時間: 2021-11-20 18:17
'所以 xD(T1) = xD(T1) + 1 只是在確定 日期|料號|批號 是不是全新組合!後方排除重複! @2
If m = 0 Then
'↑如果m數字變數是0 ??(迴圈跑到 料號|批號 是第一次在字典裡查這key m才會是0)
n = n + 1
'↑令n數字變數開始累加1 這是要放Brr陣列結果的列位,如下方 @1標註位置
'一開始n初始值是0
'這是要新增一筆 全新組合的 料號|批號 放Brr陣列結果的列位
m = n
'↑令 m數字變數值=n數字變數值
'n是要繼續累加!
'所以也要有個變數,裝現在迴圈 料號|批號 放Brr陣列結果的列位 的列號
xD(T) = n
'↑令以 料號|批號 變數為key的item= n變數值
Brr(n, 1) = Arr(i, 2) '@1
'↑將迴圈列第二欄Arr陣列位置的值倒入 Brr陣列(n數字變數值列,第一欄)位置
Brr(n, 2) = Arr(i, 3) '@1
'↑將迴圈列第三欄Arr陣列位置的值倒入 Brr陣列(n數字變數值列,第二欄)位置
End If
If xD(T1) = 1 Then '@2
'↑如果 日期|料號|批號 字串變數為key的item 等於 1
'雖然前面 都有把 料號|批號 放Brr陣列結果的列位 的列號m帶出來!
'但是 日期|料號|批號 如果重複了!這條件是不會成立的!
Brr(m, 3) = Brr(m, 3) + 1
'↑讓 Brr陣列(m數字變數值列,第三欄)位置的值累加1
End If
Next
[02!g1:i1] = [{"料號","批號","天數"}]
'↑令表二儲存格[G1:I1]依序倒入標題 "料號","批號","天數"
'又學到了!以前都只會 [02!G1:I1] = Array("料號", "批號", "天數")
With [02!g2].Resize(n, 3)
'↑以下是關於表二[G2]儲存格向下擴展n列,向右擴展3欄的範圍儲存格(以下稱:結果格)
.Value = Brr
'↑把Brr陣列的值倒入結果格
.Sort KEY1:=.Item(1), Order1:=1, _
Key2:=.Item(2), Order2:=1, Header:=2
'↑結果格做排序
'以前都以為是指定哪一儲存格做KEY1:,指定哪一儲存格做KEY2:
'原來是抓排序儲存格的欄位而已
End With
End Sub
Sub 二層次_漸增排序()
Dim xA
Set xA = [G2:I7]
xA.Sort _
KEY1:=xA.Item(1), Order1:=xlAscending, _
Key2:=xA.Item(2), Order2:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, SortMethod:=xlStroke, _
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
End Sub