【发布时间】:2019-05-02 16:18:49
【问题描述】:
我想创建一个按钮,当用户按下它时,它将执行以下操作:
- 在 Eff 工作表上的数据透视表中按下按钮用户标记名称之前
- 按下按钮后,相同名称将在工作表 Pivot_All 上的另一个数据透视表中进行过滤
我试过这个,但在 .PivotItems(a(g)).Visible = True 它给了我对象需要的错误
Public Function GetLength(a As Variant) As Integer
If IsEmpty(a) Then
GetLength = 0
Else
GetLength = UBound(a) - LBound(a) + 1
End If
GL = GetLength
End Function
Public Function a(ByVal rng As Range) As Variant
Dim f As Long, r As Range
ReDim arr(1 To rng.Count)
f = 1
For Each r In rng.Cells
arr(f) = r.Value
f = f + 1
Next r
a = arr
End Function
Sub Macro6()
Dim rngCopy As Range
Dim rngPaste As Range
Dim rng As Range
Set rng = Selection
Sheets("Pivot_All").Activate
ActiveSheet.PivotTables("PivotTable1").PivotFields("Empl").ClearAllFilters
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Empl")
For i = 1 To .PivotItems.Count - 1
.PivotItems(.PivotItems(i).Name).Visible = False
Next i
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Empl")
For g = 0 To GL
.PivotItems(a(g)).Visible = True
Next g
End With
End Sub
【问题讨论】:
-
a正在等待范围a(ByVal rng As Range)但您提交了一个数字g作为参数。您必须提交Range对象。 -
你的意思是像
For Each cell In a .PivotItems(a(cell)).Visible = True Next cell这样的东西吗? -
在下面查看我编辑的答案。
标签: excel vba pivot-table