我已经创建了一个VBA代码,可以显示包含所有字段的数据透视表,然后进行计算。 为了继续我的代码,我有一个问题,使字段显示值。
这是我已编码的VBA代码(我使用的枢轴字段是Dryness + Absorbency):
Sub create()
'PURPOSE: Creates a brand new Pivot table on a new worksheet from data in the ActiveSheet
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
Dim cht As Object
'Determine the data range you want to pivot
SrcData = ActiveSheet.Name & "!" & Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1)
'Create a new worksheet
Set sht = Sheets.Add
'Where do you want Pivot Table to start?
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:="PivotTable")
ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables( _
"PivotTable").PivotFields("Dryness + Absorbency"), _
"Count of Dryness + Absorbency", xlCount
With ActiveSheet.PivotTables("PivotTable").PivotFields("Dryness + Absorbency")
.PivotItems("NEUTRAL").Visible = False
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Subject Name")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Dryness + Absorbency")
.Orientation = xlRowField
.Position = 1
End With
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.FormulaR1C1 = "DIFFERENCE%"
Range("B8").Select
ActiveCell.FormulaR1C1 = "=ABS(R[-2]C-R[-3]C)/R[-1]C*100"
Selection.Copy
Dim lngLastColumn As Long
lngLastColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Range(ActiveCell, Cells(ActiveCell.Row, lngLastColumn)).FillRight
End Sub
以下是我需要循环使用的Excel列或数据透视表字段的摘录:
当我在其他领域尝试上述代码时,"舒适计数"列不会出现。
下方显示错误。
正如您所看到的,"舒适计数"列是空白的。它的值为" 1" for POSITIVE。
以下是预期的输出。
我被困了一段时间。任何帮助或使其更简单的方法都值得赞赏。