|
Sub limonet()
Dim Cn As Object, StrSQL$, Xrr As Variant, Yrr As Variant, Arr As Variant, Brr As Variant, Crr As Variant, Rst As Object, Rng As Range, i%
Xrr = Array("Total", "借", "贷")
Set Cn = CreateObject("Adodb.Connection")
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
StrSQL = "Select 9999 As Class,'全部合计' As Total,Sum(借方金额) as 借,Sum(贷方金额) As 贷 From [Sheet1$]"
StrSQL = StrSQL & " Union All Select Year(凭证日期) As Class,'本年累计',Sum(借方金额),Sum(贷方金额) From [Sheet1$] Group By Year(凭证日期)"
StrSQL = StrSQL & " Union All Select 月份,'本月合计',Sum(借方金额),Sum(贷方金额) From [Sheet1$] Group By 月份"
Set Rst = Cn.Execute(StrSQL)
Yrr = Rst.GetRows(, , "Class")
Rst.Filter = "Class=9999": Crr = Application.Transpose(Rst.GetRows(, , Xrr))
For i = 4 To UBound(Yrr, 2)
If i < UBound(Yrr, 2) Then
Set Rng = Cells.Find(Yrr(0, i + 1))
Else
Set Rng = Cells(Range("B" & Rows.Count).End(xlUp).Row + 1, "B")
End If
Rng.EntireRow.Resize(3).Insert
Rst.Filter = "Class=" & Left(Yrr(0, i), 4): Brr = Application.Transpose(Rst.GetRows(, , Xrr))
Rst.Filter = "Class=" & Yrr(0, i): Arr = Application.Transpose(Rst.GetRows(, , Xrr))
Rng.Offset(-3, 2).Resize(1, 3) = Arr: Rng.Offset(-2, 2).Resize(1, 3) = Brr: Rng.Offset(-1, 2).Resize(1, 3) = Crr
Next i
End Sub |
评分
-
2
查看全部评分
-
|