使用值过滤时,如何在 Excel VBA pivot table 中使用变量?
How do I use a variable in Excel VBA pivot table when using value filtering?
我有以下子项:
Sub test()
DeviceModel = "123 Model_name"
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release").AutoSort _
xlDescending, "Count of Device ID"
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release"). _
PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
PivotTables(DeviceModel).PivotFields("Count of Device ID"), Value1:=10
End Sub
这部分有效:
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release").AutoSort _
xlDescending, "Count of Device ID"
但第二部分没有:
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release"). _
PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
PivotTables(DeviceModel).PivotFields("Count of Device ID"), Value1:=10
如果我将 (DeviceModel) 替换为 ("123 Model_name"),它会按预期工作:
ActiveSheet.PivotTables("123 Model_name").PivotFields("Baseline Release"). _
PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
PivotTables("123 Model_name").PivotFields("Count of Device ID"), Value1:=10
上面写着:"application defined or object defined error"
我已经进行了大量的谷歌搜索,但仍然无法弄清楚为什么它不适用于变量而不是实际的 table 名称。
我在 Excel VBA - Pivot Table with Multiple Value Filters
中找到了答案
Dim pvt As PivotTable
Set pvt = ActiveSheet.PivotTables(DeviceModel)
With pvt.PivotFields("Baseline Release")
.ClearAllFilters
.PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=pvt.PivotFields("Count of Device ID"), Value1:=10
End With
我有以下子项:
Sub test()
DeviceModel = "123 Model_name"
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release").AutoSort _
xlDescending, "Count of Device ID"
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release"). _
PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
PivotTables(DeviceModel).PivotFields("Count of Device ID"), Value1:=10
End Sub
这部分有效:
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release").AutoSort _
xlDescending, "Count of Device ID"
但第二部分没有:
ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release"). _
PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
PivotTables(DeviceModel).PivotFields("Count of Device ID"), Value1:=10
如果我将 (DeviceModel) 替换为 ("123 Model_name"),它会按预期工作:
ActiveSheet.PivotTables("123 Model_name").PivotFields("Baseline Release"). _
PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
PivotTables("123 Model_name").PivotFields("Count of Device ID"), Value1:=10
上面写着:"application defined or object defined error"
我已经进行了大量的谷歌搜索,但仍然无法弄清楚为什么它不适用于变量而不是实际的 table 名称。
我在 Excel VBA - Pivot Table with Multiple Value Filters
中找到了答案Dim pvt As PivotTable
Set pvt = ActiveSheet.PivotTables(DeviceModel)
With pvt.PivotFields("Baseline Release")
.ClearAllFilters
.PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=pvt.PivotFields("Count of Device ID"), Value1:=10
End With