raw data 如附件所示:
[attach]2297[/attach][attach]2297[/attach]
我寫的程式如下,缺CHAMID 計算項目個數
Dim r As Single
r = [F65536].End(xlUp).Row
Set ptcache = ThisWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:="total glass!R1C2: R3030C12")
Set pt = ptcache.CreatePivotTable(TableDestination:="", TableName:="pivottable1")
With pt
.PivotFields("CHAMID").Orientation = xlRowField
.PivotFields("TRANSDT").Orientation = xlColumnField
.PivotFields("DEFQTY").Orientation = xlDataField
End With
End Sub作者: kimbal 時間: 2010-8-11 00:14
Dim r As Single
Dim a1 As Range
Dim a2 As Range
Set a1 = Range("N2") 'row field
Set a2 = Range("O2") 'column field
r = [F65536].End(xlUp).Row
Set ptcache = ThisWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:="total glass!R1C2: R3030C12")
Set pt = ptcache.CreatePivotTable(TableDestination:="", TableName:="pivottable1")
With pt
.PivotFields(Range("N2")).Orientation = xlColumnField
.AddDataField pt.PivotFields("DEFQTY"), "defect 數", xlSum
.AddDataField pt.PivotFields("CHAMID"), "glass 數", xlCount
.CalculatedFields.Add "density", "DEFQTY/CHAMID"
.PivotFields("density").Orientation = xlDataField
.Caption = " density"
.PivotFields(Range("O2")).Orientation = xlRowField
.PivotFields("CHAMID").Position = 1
End With
End Sub作者: kimbal 時間: 2010-8-14 15:47
Oh sorry 式暫試試這個?
Dim r As Single
Dim a1 As Range
Dim a2 As Range
Set a1 = Range("N2") 'row field
Set a2 = Range("O2") 'column field
r = [F65536].End(xlUp).Row
Set ptcache = ThisWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:="total glass!R1C2: R3030C12")
Set pt = ptcache.CreatePivotTable(TableDestination:="", TableName:="pivottable1")