Option Explicit
Sub 項相分類重整_20221222_1()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim Arr, i&, j&, Brr, Y, N, Ra, Sh
Set Y = CreateObject("Scripting.Dictionary")
Set Sh = Sheets("結果")
Arr = Range([分類帳!H1], [分類帳!A1].Cells(Rows.Count, 1).End(xlUp))
With Sheets.Add
With .[A1].Resize(UBound(Arr), UBound(Arr, 2))
.Value = Arr
.Sort _
KEY1:=.Item(1), Order1:=xlAscending, _
Key2:=.Item(2), Order2:=xlAscending, _
Header:=xlYes, Orientation:=xlTopToBottom
Arr = .Value
End With
.Delete
End With
For i = 1 To UBound(Arr)
Y(Arr(i, 1)) = ""
Next
ReDim Brr(1 To UBound(Arr) + Y.Count * 3, 1 To UBound(Arr, 2))
Set Ra = Sh.[A1:H1]
For i = 2 To UBound(Arr)
If Arr(i, 1) <> Arr(i - 1, 1) Then
N = IIf(i = 2, N + 1, N + 2)
Set Ra = Union(Ra, Range(Sh.Cells(N, 1), Sh.Cells(N, 8)))
Brr(N, 2) = Arr(i, 1)
N = N + 1
Set Ra = Union(Ra, Range(Sh.Cells(N, 1), Sh.Cells(N, 8)))
For j = 1 To UBound(Arr, 2)
Brr(N, j) = Arr(1, j)
Next
End If
111
N = N + 1
Set Ra = Union(Ra, Range(Sh.Cells(N, 1), Sh.Cells(N, 8)))
For j = 1 To UBound(Arr, 2)
Brr(N, j) = Arr(i, j)
Next
Brr(N, 2) = "'" & Format(Brr(N, 2), "yyyy-mm-dd")
Brr(N, 3) = "'" & Brr(N, 3)
Next
Sh.UsedRange.ClearContents
Sh.Cells.Borders.LineStyle = 0
Ra.Borders.LineStyle = 1
Sh.[A1].Resize(UBound(Brr), UBound(Brr, 2)) = Brr
Set Y = Nothing
Set Arr = Nothing
Set Brr = Nothing
End Sub作者: mdr0465 時間: 2022-12-22 21:07
Sub 分類()
With CreateObject("adodb.connection"): V = Application.Version:
If V >= 12 Then V = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0; "
If V < 12 Then V = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0; "
.Open V & "Data Source=" & ThisWorkbook.FullName
Set s = Sheets("結果"): Set s1 = Sheets("分類帳")
ar = s1.Range("b1:H1")
tx = Join(Application.Index(ar, 1, 0), ",")
Set rs = .Execute("select distinct " & s1.[A1] & " from [分類帳$A1:A]")
rr = rs.getrows(, , "明細科目_幣別")
s.Cells.ClearContents
For Each Z In rr
r = s.Cells(Rows.Count, 1).End(3).Row + 2
s.Cells(r, 1) = Z
s.Cells(r + 1, 1).Resize(1, UBound(ar, 2)) = ar
q = "select " & tx & " from [分類帳$A1:H] where 明細科目_幣別 = '" & Z & "' and 摘要 not like '%本%日%合%計%' and 摘要 not like '%本%年%累%計%'"
s.Cells(r + 2, 1).CopyFromRecordset .Execute(q)
Next
s.Rows("1:2").Delete Shift:=xlUp
r = s.Cells(Rows.Count, 1).End(3).Row
s.Cells(1, 1).Resize(r, 7).Borders.LineStyle = 1
End With
End Sub作者: Andy2483 時間: 2023-3-6 14:14