Vba 透视报告筛选器-如果只有一个值可用,请选择“全部”
请求帮助我目前正在处理一个基于宏的透视表,其中报告过滤器中有5列。 我要找的是这五个文件服务器的vba代码,如果过滤器包含单个值,它应该显示该值,否则应该保持为(全部) 目前,我正在为pivot filer使用以下代码:Vba 透视报告筛选器-如果只有一个值可用,请选择“全部”,vba,filter,pivot,Vba,Filter,Pivot,请求帮助我目前正在处理一个基于宏的透视表,其中报告过滤器中有5列。 我要找的是这五个文件服务器的vba代码,如果过滤器包含单个值,它应该显示该值,否则应该保持为(全部) 目前,我正在为pivot filer使用以下代码: Sub InsertPivotTable() 'Declare Variables Dim PSheet As Worksheet Dim DSheet As Worksheet Dim PCache As PivotCache Dim PTable As Pivot
Sub InsertPivotTable()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Pivot").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Pivot"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Pivot")
Set DSheet = Worksheets("Report")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="ADT_PivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="ADT_PivotTable")
'Insert Reportfilter Fields
With ActiveSheet.PivotTables("ADT_PivotTable").PivotFields("Resp Bus Partn ID")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("ADT_PivotTable").PivotFields("ADT-File ID")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("ADT_PivotTable").PivotFields("UWY")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("ADT_PivotTable").PivotFields("SCoB - Acc")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("ADT_PivotTable").PivotFields("Curr")
.Orientation = xlPageField
.Position = 1
End With
End Sub
答案和代码有点长,但我为您添加了一些“奖金”,因此您会发现它非常有用:) 首先,不需要删除“数据透视”表并重新创建它,只需更新名为“ADT_数据透视表”的
数据透视表,您只需使用更新的源数据
更新数据透视缓存
,然后使用更新的数据透视缓存
刷新数据透视表
第二个,我添加了第二个子
,检查传递给它的每个数据透视字段
有多少数据透视项
,如果只有一个数据透视项
,则在过滤器中显示它,否则显示“全部”
子插入数据透视表代码
Option Explicit
Sub InsertPivotTable()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
On Error Resume Next
Application.DisplayAlerts = False
Set PSheet = Worksheets("Pivot")
On Error GoTo 0
If PSheet Is Nothing Then ' if "Pivot" sheet doesn't exist
Set PSheet = Sheets.Add(Before:=ActiveSheet)
PSheet.Name = "Pivot"
End If
Application.DisplayAlerts = True
Set DSheet = Worksheets("Report")
'Define Data Range
With DSheet
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set PRange = .Range("A1").Resize(LastRow, LastCol) ' set data range for Pivot Table
End With
' Set the Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
On Error Resume Next
Set PTable = PSheet.PivotTables("ADT_PivotTable") ' Set the Pivot Table if already exists from previous code runs
On Error GoTo 0
If PTable Is Nothing Then ' <-- Pivot Table still doesn't exist >> create it for the first time
' create a new Pivot Table in "Pivot" sheet
Set PTable = PSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=PSheet.Range("A1"), TableName:="ADT_PivotTable")
With PTable
'Insert Reportfilter Fields
With .PivotFields("Resp Bus Partn ID")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("ADT-File ID")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("UWY")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("SCoB - Acc")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("Curr")
.Orientation = xlPageField
.Position = 1
End With
End With
Else
' just refresh the Pivot cache with the updated Range
PTable.ChangePivotCache PCache
PTable.RefreshTable
End If
' modify the Filter default view for each of the Pivot Fields below
With PTable
SelectFilterSingleValue .PivotFields("Resp Bus Partn ID")
SelectFilterSingleValue .PivotFields("ADT-File ID")
SelectFilterSingleValue .PivotFields("UWY")
SelectFilterSingleValue .PivotFields("SCoB - Acc")
SelectFilterSingleValue .PivotFields("Curr")
End With
End Sub
Sub SelectFilterSingleValue(ptFld As PivotField)
Dim PTItm As PivotItem
Dim Count As Long
With ptFld
.EnableMultiplePageItems = True
For Each PTItm In .PivotItems
If PTItm.Name <> "(blank)" Then
PTItm.Visible = True
Count = Count + 1
Else
PTItm.Visible = False
End If
Next PTItm
If Count > 1 Then
.ClearAllFilters
End If
End With
End Sub
选项显式
子插入数据透视表()
'声明变量
将PSheet设置为工作表
将数据表设置为工作表
Dim PCache作为数据透视缓存
数据透视表
昏昏欲睡
最后一排一样长
暗淡如长
'删除以前的透视表工作表并插入一个同名的新空白工作表
出错时继续下一步
Application.DisplayAlerts=False
设置PSheet=工作表(“轴”)
错误转到0
如果PSheet不存在,则“Pivot”表不存在
设置PSheet=Sheets.Add(之前:=ActiveSheet)
PSheet.Name=“Pivot”
如果结束
Application.DisplayAlerts=True
设置数据表=工作表(“报告”)
'定义数据范围
带数据表
LastRow=.Cells(.Rows.Count,1).End(xlUp).Row
LastCol=.Cells(1,.Columns.Count).End(xlToLeft).Column
Set PRange=.Range(“A1”).Resize(LastRow,LastCol)”设置数据透视表的数据范围
以
'设置透视缓存
设置PCache=ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase,SourceData:=PRange)
出错时继续下一步
Set PTable=PSheet.PivotTables(“ADT_PivotTable”)“如果在以前的代码运行中已经存在数据透视表,则设置该数据透视表
错误转到0
如果PTable什么都不是,那么“答案和代码有点长,但我为您添加了一些“奖金”,因此您会发现它非常有用:)
首先,不需要删除“数据透视”表并重新创建它,只需更新名为“ADT_数据透视表”的数据透视表,您只需使用更新的源数据
更新数据透视缓存
,然后使用更新的数据透视缓存
刷新数据透视表
第二个,我添加了第二个子
,检查传递给它的每个数据透视字段
有多少数据透视项
,如果只有一个数据透视项
,则在过滤器中显示它,否则显示“全部”
子插入数据透视表代码
Option Explicit
Sub InsertPivotTable()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
On Error Resume Next
Application.DisplayAlerts = False
Set PSheet = Worksheets("Pivot")
On Error GoTo 0
If PSheet Is Nothing Then ' if "Pivot" sheet doesn't exist
Set PSheet = Sheets.Add(Before:=ActiveSheet)
PSheet.Name = "Pivot"
End If
Application.DisplayAlerts = True
Set DSheet = Worksheets("Report")
'Define Data Range
With DSheet
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set PRange = .Range("A1").Resize(LastRow, LastCol) ' set data range for Pivot Table
End With
' Set the Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
On Error Resume Next
Set PTable = PSheet.PivotTables("ADT_PivotTable") ' Set the Pivot Table if already exists from previous code runs
On Error GoTo 0
If PTable Is Nothing Then ' <-- Pivot Table still doesn't exist >> create it for the first time
' create a new Pivot Table in "Pivot" sheet
Set PTable = PSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=PSheet.Range("A1"), TableName:="ADT_PivotTable")
With PTable
'Insert Reportfilter Fields
With .PivotFields("Resp Bus Partn ID")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("ADT-File ID")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("UWY")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("SCoB - Acc")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("Curr")
.Orientation = xlPageField
.Position = 1
End With
End With
Else
' just refresh the Pivot cache with the updated Range
PTable.ChangePivotCache PCache
PTable.RefreshTable
End If
' modify the Filter default view for each of the Pivot Fields below
With PTable
SelectFilterSingleValue .PivotFields("Resp Bus Partn ID")
SelectFilterSingleValue .PivotFields("ADT-File ID")
SelectFilterSingleValue .PivotFields("UWY")
SelectFilterSingleValue .PivotFields("SCoB - Acc")
SelectFilterSingleValue .PivotFields("Curr")
End With
End Sub
Sub SelectFilterSingleValue(ptFld As PivotField)
Dim PTItm As PivotItem
Dim Count As Long
With ptFld
.EnableMultiplePageItems = True
For Each PTItm In .PivotItems
If PTItm.Name <> "(blank)" Then
PTItm.Visible = True
Count = Count + 1
Else
PTItm.Visible = False
End If
Next PTItm
If Count > 1 Then
.ClearAllFilters
End If
End With
End Sub
选项显式
子插入数据透视表()
'声明变量
将PSheet设置为工作表
将数据表设置为工作表
Dim PCache作为数据透视缓存
数据透视表
昏昏欲睡
最后一排一样长
暗淡如长
'删除以前的透视表工作表并插入一个同名的新空白工作表
出错时继续下一步
Application.DisplayAlerts=False
设置PSheet=工作表(“轴”)
错误转到0
如果PSheet不存在,则“Pivot”表不存在
设置PSheet=Sheets.Add(之前:=ActiveSheet)
PSheet.Name=“Pivot”
如果结束
Application.DisplayAlerts=True
设置数据表=工作表(“报告”)
'定义数据范围
带数据表
LastRow=.Cells(.Rows.Count,1).End(xlUp).Row
LastCol=.Cells(1,.Columns.Count).End(xlToLeft).Column
Set PRange=.Range(“A1”).Resize(LastRow,LastCol)”设置数据透视表的数据范围
以
'设置透视缓存
设置PCache=ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase,SourceData:=PRange)
出错时继续下一步
Set PTable=PSheet.PivotTables(“ADT_PivotTable”)“如果在以前的代码运行中已经存在数据透视表,则设置该数据透视表
错误转到0
如果PTable什么都不是,那么“您的代码有错误吗?”?如果是,哪一行?@ShaiRado否我目前没有收到任何错误。默认情况下,Pivot将值显示为(全部),在上述5个过滤器中,大多数过滤器中只有一个值,不需要过滤器。但是,由于商定的标准格式目的,我们无法从该过滤器中删除标准,但需要手动更改。因此,我一直在寻找的是,如果数据透视过滤器只包含一个或单个值,那么它应该自动显示该值,否则作为默认值(全部),您的代码是否有错误?如果是,哪一行?@ShaiRado否我目前没有收到任何错误。默认情况下,Pivot将值显示为(全部),在上述5个过滤器中,大多数过滤器中只有一个值,不需要过滤器。但是,由于商定的标准格式目的,我们无法从该过滤器中删除标准,但需要手动更改。因此,我一直在寻找的是,如果数据透视筛选器只包含一个或单个值,则应自动显示该值,否则为默认值(全部)。非常感谢您的努力:)并尝试使用代码,但“Sub-SelectFilterSingleValue代码”对我无效。没有得到任何错误,但它没有反映在数据透视中。@Sanoj可能我不明白您想要什么,因为它为我的过滤器做了一些事情(可能不是您想要的),您能编辑您的帖子并添加一个屏幕截图,说明您希望如何数据透视表
和过滤器的外观(甚至手动)我在最初的帖子中添加了一个屏幕截图。在这里,您可以看到一些带有数字的过滤器,一些过滤器是(全部)。此处,(All)是默认值,因为此筛选器在下拉列表中包含多个值。其中作为第一个过滤器,我