0
比例公式,我想計算成功的比例在Excel數據透視表。這是我的設計:的數據透視表
Row Labels Count Threshold-%
REG1 224 0.00%
FALSE 11
TRUE 213
REG2 213 0.00%
FALSE 13
TRUE 200
REG3 318 0.00%
FALSE 3
TRUE 315
REG4 467 0.00%
FALSE 7
TRUE 460
Grand Total 1222 0.00%
,這是我的VBA代碼:
Sub CreatePivotTable()
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
Dim lRow As Long
Dim pf_name As String
pf_name = "Count of State"
lRow = Cells(Rows.Count, 12).End(xlUp).Row
'Data Range for Pivot Table
SrcData = ActiveSheet.Name & "!" & Range("A1:L" & lRow).Address(ReferenceStyle:=xlR1C1)
'Set Worksheet
Set sht = Worksheets("Metrics")
'Pivot Table Starting Point
StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create(_
SourceType:=xlDatabase, _
SourceData:=SrcData)
'Create Pivot Table From Pivot Cache
Set pvt = pvtCache.CreatePivotTable(_
TableDestination:=StartPvt, _
TableName:="PivotTable1")
Set pvt = Worksheets("Metrics").PivotTables("PivotTable1")
'Define Rows
sht.PivotTables("PivotTable1").AddFields RowFields:=Array("Region", "State")
'Create Count Column
With pvt.PivotFields("State")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
.NumberFormat = "0"
End With
'Create Calculated Column
pvt.CalculatedFields.Add Name:="Threshold", Formula:="=SUM(True/True+False)"
'Populate Calculated Column
With pvt.PivotFields("Threshold")
.Orientation = xlDataField
.Function = xlSum
.Position = 2
.NumberFormat = "0.00%"
.Caption = "Threshold-%"
End With
'Show Grand Totals
pvt.ColumnGrand = True
pvt.RowGrand = True
End Sub
當我奮力創造的公式來執行我想要的設計。這是我用過的線:
'Create Calculated Column
pvt.CalculatedFields.Add Name:="Threshold", Formula:="=SUM(TRUE/TRUE+FALSE)"
它給我的所有東西都是100%。我如何訪問TRUE和FALSE值,以便我可以在正確的公式中使用它們?
另外,我只想要顯示的總百分比(因此P%= TRUE /(TRUE + FALSE)* 100)針對每個區域,而不顯示百分比總計TRUE或FALSE。然後我想顯示區域百分比的平均值總計行中,像這樣的:
Row Labels Count Threshold-%
REG1 224 95.09%
FALSE 11
TRUE 213
REG2 213 93.90%
FALSE 13
TRUE 200
REG3 318 99.06%
FALSE 3
TRUE 315
REG4 467 98.50%
FALSE 7
TRUE 460
Grand Total 1222 96.64%
我很抱歉,我的問題沒有得到沒有我想象的那麼清楚。該代碼的工作,但只給我看了國家總的計數,然後是總的從總計的百分比。我所試圖找出是每個地區的成功率(基於true和false值),然後獲取這些結果的平均百分比的總計行中顯示 –
我已編輯的問題,以顯示我是什麼尋求幫助與 –
我不知道的方式做到這一點與數據透視表,你可能不得不使用普通的公式,或者用VBA – tigeravatar