以下是引用daley_huang在2007-7-24 11:08:54的发言:还是那个问题:怎样添加列,使这列的值是某一列的汇总值 (数据行数是不确定的,是从数据库中取出的,每次取出就是一个Excel文件,我会通过录制宏先把透视表做好,每次取出数据也就是说产生Excel文件,只要点击 一个按钮(自己写的,去调用那个宏)就会自动产生透视表)
给你写了一个宏,应该可以解决你的问题了. Sub CreatePivot() Dim row%, source$, sumA As Long, sumB As Long, sumC As Long, strA$, strB$, strC$ With Sheets("test") .Select row = [A65536].End(xlUp).row source = "test!R1C1:R" & row & "C5" sumA = Application.WorksheetFunction.Sum(.Range("C2:C65536")) sumB = Application.WorksheetFunction.Sum(.Range("D2:D65536")) sumC = Application.WorksheetFunction.Sum(.Range("D2:D65536")) End With strA = "=入库量 /" & sumA strB = "=入库量 /" & sumB strC = "=配发量 /" & sumC ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _ source).CreatePivotTable TableDestination:="", TableName:= _ "数据透视表1", DefaultVersion:=xlPivotTableVersion10 With ActiveSheet.PivotTables("数据透视表1") .PivotFields("款号").Orientation = xlRowField .AddDataField .PivotFields("入库量"), "求和项:入库量", xlSum .AddDataField .PivotFields("配发量"), "求和项:配发量", xlSum .AddDataField .PivotFields("销售量"), "求和项:销售量", xlSum .DataPivotField.Orientation = xlColumnField .CalculatedFields.Add "A", strA, True .CalculatedFields.Add "B", strB, True .CalculatedFields.Add "C", strC, True .PivotFields("A").Orientation = xlDataField .PivotFields("B").Orientation = xlDataField .PivotFields("C").Orientation = xlDataField .PivotFields("求和项:A").NumberFormat = "0.00%" .PivotFields("求和项:B").NumberFormat = "0.00%" .PivotFields("求和项:C").NumberFormat = "0.00%" End With End Sub
[此贴子已经被作者于2007-7-24 14:50:54编辑过] |