|
楼主 |
发表于 2011-3-28 05:23
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
这是我根据上面的问题做的VBA,我改用逻辑为遍历所有items,并对比他们与我需要的items的值,如果不是将这些items都隐藏起来。
Sub Macro2()
'
' Macro2 Macro
' 宏由 微软用户 录制,时间: 2011-3-26
'
'
ActiveWorkbook.Names.Add Name:="database1", RefersToR1C1:= _
"=OFFSET(Sheet1!R1C1,0,0,COUNTA(Sheet1!C1),3)"
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"database1").CreatePivotTable TableDestination:="", TableName:="数据透视表2", _
DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet.PivotTables("数据透视表2").PivotFields("Name")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("数据透视表2").PivotFields("Sex")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("数据透视表2").AddDataField ActiveSheet.PivotTables("数据透视表2" _
).PivotFields("Score"), "求和项:Score", xlSum
Dim i As Integer, j As Integer
i = 1
j = ActiveSheet.PivotTables("数据透视表2").PivotFields("Sex").PivotItems.Count
For i = 1 To j
If ActiveSheet.PivotFields("Sex").PivotItems(i).Value <> "F" And ActiveSheet.PivotFields("Sex").PivotItems(i).Value <> "M" Then
With ActiveSheet.PivotTables("数据透视表2").PivotFields("Sex")
.PivotItems(i + 1).Visible = False
End With
End If
Next
End Sub
问题是运行时显示橘色这部分,对象不支持该属性或方法,错误为438.我想是不是Pivotitems(i).value<>"F", 不该这么表达呢? |
|