|
Sub aa()
起始月份 = Cells(1, 2): 终止月份 = Cells(2, 2)
科目起始代码 = Cells(1, 3): 科目终止代码 = Cells(2, 3)
Application.DisplayAlerts = False
Set cn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
cn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source= " & ThisWorkbook.FullName
Sql = "SELECT * FROM [流水账$a3:l] where 月 between " & 起始月份 & " and " & 终止月份 & "" 'and left(二级科目代码,7) between " & 科目起始代码 & " and " & 科目终止代码 & ""
Set rs.ActiveConnection = cn
rs.Open Sql
Set pvc = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
Set pvc.Recordset = rs
Set pvt = ActiveSheet.PivotTables.Add(PivotCache:=pvc, TableDestination:=Range("m10"))
With pvt
.SmallGrid = False
.AddFields RowFields:="摘要说明", ColumnFields:="三级科目"
.AddDataField .PivotFields("借方金额"), "借方金额求和", xlSum
.RowGrand = False
End With
End Sub |
|