試試看
Sub ex()
Dim arr
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
arr = Sheets("資料").[B5].CurrentRegion
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, 2) & arr(i, 8)) Then
d.Add (arr(i, 2) & arr(i, 8)), 1
Else
d(arr(i, 2) & arr(i, 8)) = d(arr(i, 2) & arr(i, 8)) + 1
End If
Next
With Sheets(1)
arr = .[B6].CurrentRegion
For i = 2 To UBound(arr, 2) - 1
For j = 2 To UBound(arr)
If d(arr(j, 1) & arr(1, i)) = "" Then
arr(j, i) = 0
Else
arr(j, i) = d(arr(j, 1) & arr(1, i))
End If
Next
Next
.[B6].CurrentRegion = arr
.[L7].Resize(UBound(arr) - 1) = "=sum(c7:K7)"
.[L:L] = .[L:L].Value
End With
Set d = Nothing
End Sub作者: b9208 時間: 2020-8-21 12:31
Sub TEST()
Dim Arr, Brr, xD, i&, j%, K, R&, C&, N&
Set xD = CreateObject("Scripting.Dictionary")
[工作表2!B7:B2000].EntireRow.Delete
Arr = [工作表2!B6:K6]
For j = 2 To UBound(Arr, 2): xD(Arr(1, j)) = j: Next
Arr = Range([資料!A1], Sheets("資料").UsedRange)
ReDim Brr(1 To UBound(Arr), 1 To 11)
For i = 5 To UBound(Arr)
K = Arr(i, 2): R = Val(xD(K)): C = Val(xD(Arr(i, 8)))
If K = "" Or C = 0 Then GoTo 101
If R = 0 Then N = N + 1: R = N: xD(K) = N: Brr(N, 1) = K
Brr(R, C) = Brr(R, C) + 1: Brr(R, 11) = Brr(R, 11) + 1
101: Next i
With [工作表2!B7].Resize(N, 11)
.Value = Brr
.Borders.LineStyle = 1
End With
End Sub
Sub 統計入口()
[B6].CurrentRegion.Offset(2).Clear
統計 [B6], 8
End Sub
Sub 統計出口()
[P6].CurrentRegion.Offset(2).Clear
統計 [P6], 9
End Sub
Sub 統計(ByVal cel0 As Range, Ci As Long)
Dim D, Arr, Brr, T$, K1$, K2$, Key, R&, Ro&, Co&, Rg As Range
Set D = CreateObject("Scripting.Dictionary")
Arr = [資料!A4].CurrentRegion
For R = 2 To UBound(Arr)
K1 = Arr(R, 2): K2 = Arr(R, Ci)
If K1 <> T Then Ro = Ro + 1: D(K1) = Ro: T = K1
If K2 <> "" Then Key = K1 & "-" & K2: D(Key) = D(Key) + 1
Next
ReDim Brr(1 To Ro, 1 To 11)
For Each Key In D.keys
If InStr(Key, "-") = 0 Then Brr(D(Key), 1) = Key: GoTo 下個Key
Ro = D(Split(Key, "-")(0))
Set Rg = cel0.Resize(, 10).Find(Split(Key, "-")(1), , , xlWhole)
If Not Rg Is Nothing Then
Co = Rg.Column - cel0.Column + 1
Brr(Ro, Co) = D(Key): Brr(Ro, 11) = Brr(Ro, 11) + D(Key)
End If
下個Key: Next
With cel0(2).Resize(Ro, 11)
.Value = Brr: .Borders.LineStyle = 1
.VerticalAlignment = xlBottom
.HorizontalAlignment = xlCenter
End With
End Sub
Sub TEST_T()
Dim Arr, Brr, Crr, xD, i&, K, R&, C&, N1&, N2&
Set xD = CreateObject("Scripting.Dictionary")
[工作表2!B7:B2000].EntireRow.Delete
Arr = [工作表2!B6:K6]
For i = 2 To UBound(Arr, 2): xD(Arr(1, i)) = i: Next
Arr = Range([資料!A1], Sheets("資料").UsedRange)
ReDim Brr(1 To UBound(Arr), 1 To 11): Crr = Brr
For i = 5 To UBound(Arr)
K = Arr(i, 2): If K = "" Then GoTo 101
R = Val(xD(K)): C = Val(xD(Arr(i, 8)))
If C > 0 Then
If R = 0 Then N1 = N1 + 1: R = N1: xD(K) = R: Brr(R, 1) = K
Brr(R, C) = Brr(R, C) + 1: Brr(R, 11) = Brr(R, 11) + 1
End If
'--------------------------------
R = Val(xD(K & "/")): C = Val(xD(Arr(i, 9)))
If C > 0 Then
If R = 0 Then N2 = N2 + 1: R = N2: xD(K & "/") = R: Crr(R, 1) = K
Crr(R, C) = Crr(R, C) + 1: Crr(R, 11) = Crr(R, 11) + 1
End If
101: Next i
With [工作表2!B7].Resize(N1, 11)
.Value = Brr
.Borders.LineStyle = 1
End With
With [工作表2!P7].Resize(N2, 11)
.Value = Crr
.Borders.LineStyle = 1
End With
End Sub
Sub TEST_T2()
Dim Arr, Brr, xD, i&, j%, K, R&, C&, N&(1)
Set xD = CreateObject("Scripting.Dictionary")
[工作表2!B7:B2000].EntireRow.Delete
Arr = [工作表2!B6:K6]
For i = 2 To UBound(Arr, 2): xD(Arr(1, i) & "") = i: Next
Arr = Range([資料!A1], Sheets("資料").UsedRange)
ReDim Brr(1 To UBound(Arr), 1 To 11): xD(0) = Brr: xD(1) = Brr
For i = 5 To UBound(Arr)
K = Arr(i, 2): If K = "" Then GoTo i01
For j = 0 To 1
R = xD(K & j): C = xD(Arr(i, 8 + j) & ""): Brr = xD(j)
If C = 0 Then GoTo j01
If R = 0 Then N(j) = N(j) + 1: R = N(j): xD(K & j) = R: Brr(R, 1) = K
Brr(R, C) = Brr(R, C) + 1: Brr(R, 11) = Brr(R, 11) + 1
xD(j) = Brr
j01: Next j
i01: Next i
For j = 0 To 1
With Sheets("工作表2").Range(Array("B7", "P7")(j)).Resize(N(j), 11)
.Value = xD(j)
.Borders.LineStyle = 1
End With
Next j
End Sub
Sub 統計入口()
[B6].CurrentRegion.Offset(2).Clear
統計 [B6], 8
End Sub
Sub 統計出口()
[P6].CurrentRegion.Offset(2).Clear
統計 [P6], 9
End Sub
Sub 統計(ByVal cel0 As Range, Ci As Long)
Dim D, Arr, Brr, T$, K1$, K2$, Key, R&, Ro&, Co&, Rg As Range
Set D = CreateObject("Scripting.Dictionary")
Arr = [資料!A4].Resize([資料!B4].End(4).Row - 3, 9)
For R = 2 To UBound(Arr)
K1 = Arr(R, 2): K2 = Arr(R, Ci)
If K1 <> T Then Ro = Ro + 1: D(K1) = Ro: T = K1
If K2 <> "" Then Key = K1 & "-" & K2: D(Key) = D(Key) + 1
Next
ReDim Brr(1 To Ro, 1 To 11)
For Each Key In D.keys
If InStr(Key, "-") = 0 Then Brr(D(Key), 1) = Key: GoTo 下個Key
Ro = D(Split(Key, "-")(0))
Set Rg = cel0.Resize(, 10).Find(Split(Key, "-")(1), , , xlWhole)
If Not Rg Is Nothing Then
Co = Rg.Column - cel0.Column + 1
Brr(Ro, Co) = D(Key): Brr(Ro, 11) = Brr(Ro, 11) + D(Key)
End If
下個Key: Next
With cel0(2).Resize(Ro, 11)
.Value = Brr: .Borders.LineStyle = 1
.VerticalAlignment = xlBottom
.HorizontalAlignment = xlCenter
End With
End Sub作者: b9208 時間: 2020-8-24 20:22
Option Explicit
Sub TEST_A()
Dim Brr, Crr(2000, 100), v, Z, Q, i&, j%, R&, C%, N&, U%, T2$, T8$
Set Z = CreateObject("Scripting.Dictionary")
Brr = Range(Sheet2.[H1], Sheet2.[B65536].End(3)(1, 0))
For i = 5 To UBound(Brr)
T2 = Trim(Brr(i, 2)): T8 = Trim(Brr(i, 8))
If T2 = "" Or T8 = "" Then GoTo i01
R = Z(T2): C = Z(T8): Crr(0, 0) = "日期"
If R = 0 Then N = N + 1: R = N: Z(T2) = R: Crr(R, 0) = T2
If C = 0 Then U = U + 1: C = U: Z(T8) = C: Crr(0, C) = T8
Crr(R, C) = Crr(R, C) + 1
i01: Next
Sheet1.[B6:B2000].EntireRow.Delete
With Sheet1.[B6].Resize(N + 1, U + 2)
.Value = Crr
.Offset(0, 1).Sort KEY1:=.Item(1, 2), Order1:=1, Header:=0, Orientation:=2
.Offset(1, 0).Sort KEY1:=.Item(2, 1), Order1:=1, Header:=0, Orientation:=1
.Item(1)(2, U + 2).Resize(N, 1) = "=SUM(" & Range(.Cells(2, 2), .Cells(2, U + 1)).Address(0, 0) & ")"
.Cells(1, U + 2) = "Total"
.Borders.LineStyle = 1
End With
End Sub作者: singo1232001 時間: 2023-12-8 15:36
Sub t5()
i = Split("Provider=Microsoft.,Jet.OLEDB.4,.0;Extended Properties=Excel ,8,.0;Data Source=", ",")
If Application.Version > 12 Then i(1) = "ACE.OLEDB.12": i(3) = 12
Set cn = CreateObject("adodb.connection"): cn.Open Join(i, "") & ThisWorkbook.FullName
Set S1 = Sheet1: Set s2 = Sheet2: S1.[B7:AA9999].ClearContents: