[英]excel VBA filter pivot table with flexible names
Set Weight_wb = Application.Workbooks(WEIGHTFileName).Sheets("TERRY_CAT")
Set pt = Weight_wb .PivotTables("PivotTable4")
With Weight_wb .PivotTables("PivotTable4").PivotFields("Cat")
.Orientation = xlColumnField
.Position = 1
End With
With Weight_wb .PivotTables("PivotTable4").PivotFields("Cat")
.Orientation = xlColumnField
On Error Resume Next
.PivotItems("black").Visible = True
.PivotItems("yellow").Visible = False
.PivotItems("brown").Visible = False
.Position = 1
End With
I want to change the above code to be more flexible. 我想更改上面的代码以更灵活。 I want to write a more general sub SetClolumnFilter, so that I can simply call:
我想编写一个更通用的子SetClolumnFilter,以便可以简单地调用:
SetColumnFilter WEIGHTFileName, "Terry_cat", "PivotTable4", "CAT","black"
something like (it doesnt work): 有点像(它不起作用):
Sub SetPageFilter(WB As String, WS As String, pt As String, fd As String,value as string)
Dim wb_ As Workbook, ws_ As Worksheet, pt_ As PivotTable, fd_ As PivotField
ws_ = Application.Workbooks(WB).Sheets(WS)
pt_ = ws_.PivotTables(pt)
fd_ = pt_.PivotFields(fd)
With fd_
.Orientation = xlPageField
End With
Application.ScreenUpdating = True
Dim i As Long
.PivotItems(1).Visible = True
For i = 2 To Field.PivotItems.Count
If .PivotItems(i).Name = Value Then _
.PivotItems(i).Visible = True Else _
.PivotItems(i).Visible = False
Next i
End Sub
Thanks a lot!! 非常感谢!!
Sub SetPageFilter(WB As String, WS As String, pt As String, fd As String,value as string)
Dim wb_ As Workbook, ws_ As Worksheet, pt_ As PivotTable, fd_ As PivotField
ws_ = Application.Workbooks(WB).Sheets(WS)
pt_ = ws_.PivotTables(pt)
fd_ = pt_.PivotFields(fd)
With fd_
.Orientation = xlPageField
End With
Application.ScreenUpdating = True
Dim i As Long
With fd_
.PivotItems(.PivotItems.Count).Visible = True
For i = 1 To .PivotItems.Count
If .PivotItems(i).Name = Value Then _
.PivotItems(i).Visible = True Else _
.PivotItems(i).Visible = False
Next I
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.