如何在非英语 Excel 版本中使用切片器 link Table 和枢轴 Table?
How to link a Table and a Pivot Table using Slicers in non-English Excel versions?
我正在尝试 link 从具有常规 table 的枢轴 table 切片器。所以最后当我 select 枢轴 table 切片器中的某些东西时,它也应该过滤常规 table 中的数据。
到目前为止,我遵循了此解决方案中的说明:
请在下面找到我在工作簿中使用的确切代码:
Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim sLastUndoStackItem As String
Dim sc As SlicerCache
Dim si As SlicerItem
Dim vItems As Variant
Dim i As Long
Dim lo As ListObject
Dim lc As ListColumn
Dim sTest As String
Const sPivot As String = "pivot1" '<= Change name as appropriate
Const sTable As String = "Table1" '<= Change name as appropriate
If Target.Name = sPivot Then
On Error Resume Next 'in case the undo stack has been wiped or doesn't exist
sLastUndoStackItem = Application.CommandBars(14).FindControl(ID:=128).List(1) 'Standard Commandbar, undo stack
'The above line doesn't seem to work in my version of O365 so we'll use the English language backup
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
On Error GoTo 0
If sLastUndoStackItem = "Filter" Or sLastUndoStackItem = "Slicer Operation" Then
Set lo = Sheets("Sheet1").Range(sTable).ListObject
For Each sc In ActiveWorkbook.SlicerCaches
On Error Resume Next
sTest = sc.PivotTables(1).Name
On Error GoTo 0
If sTest = sPivot Then
Set lc = lo.ListColumns(sc.SourceName)
If sc.FilterCleared Then
lo.Range.AutoFilter Field:=lc.Index
Else
ReDim vItems(1 To 1)
For Each si In sc.SlicerItems
If si.Selected Then
i = i + 1
ReDim Preserve vItems(1 To i)
vItems(i) = si.Name
End If
Next si
lo.Range.AutoFilter Field:=lc.Index, Criteria1:=vItems, Operator:=xlFilterValues
ReDim vItems(1 To 1)
End If
End If
Next sc
End If
End If
End Sub
使用此代码,我能够 select 来自枢轴 table 切片器的项目,以及常规 table 过滤器。这在英语 excel 版本中完美运行,但在其他语言(如波兰语)中不起作用。这意味着当我 select 来自波兰语 Excel 版本的数据透视 table 切片器的项目时,只有数据透视 table 过滤器,而常规 table 不受影响。我没有从 VBA 中得到任何错误。我正在寻找一种无论语言版本如何都适用的解决方案。
我想问题可能与以下线路有关:
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
我正在使用 Excel 2016.
谁能帮我解决这个问题?
尝试调查一下,找出您的代码的哪一部分没有按预期工作。
Sub Investigate()
Dim Bar As CommandBar
Dim UndoControl As CommandBarControl
Set UndoControl = Application.CommandBars(14).FindControl(ID:=128)
If UndoControl Is Nothing Then
Debug.Print "UndoControl ID 128 not found"
Set UndoControl = Application.CommandBars("Standard").Controls("&Cofnij")
If UndoControl Is Nothing Then
Debug.Print "UndoControl &Cofnij not found"
Exit Sub
End If
End If
If UndoControl.ListCount > 0 Then
Debug.Print UndoControl.List(1)
Else
Debug.Print "List is empty"
End If
End Sub
检查你得到的输出。
在这段代码之后
On Error Resume Next 'in case the undo stack has been wiped or doesn't exist
sLastUndoStackItem = Application.CommandBars(14).FindControl(ID:=128).List(1) 'Standard Commandbar, undo stack
'The above line doesn't seem to work in my version of O365 so we'll use the English language backup
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
On Error GoTo 0
您应该检查是否找到堆栈项目。因此,如果未发现任何内容,您会收到通知,而不是无声的 “什么都没有发生”.
If sLastUndoStackItem = "" Then
MsgBox "No stack item found."
Exit Sub
End If
我正在尝试 link 从具有常规 table 的枢轴 table 切片器。所以最后当我 select 枢轴 table 切片器中的某些东西时,它也应该过滤常规 table 中的数据。
到目前为止,我遵循了此解决方案中的说明:
请在下面找到我在工作簿中使用的确切代码:
Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim sLastUndoStackItem As String
Dim sc As SlicerCache
Dim si As SlicerItem
Dim vItems As Variant
Dim i As Long
Dim lo As ListObject
Dim lc As ListColumn
Dim sTest As String
Const sPivot As String = "pivot1" '<= Change name as appropriate
Const sTable As String = "Table1" '<= Change name as appropriate
If Target.Name = sPivot Then
On Error Resume Next 'in case the undo stack has been wiped or doesn't exist
sLastUndoStackItem = Application.CommandBars(14).FindControl(ID:=128).List(1) 'Standard Commandbar, undo stack
'The above line doesn't seem to work in my version of O365 so we'll use the English language backup
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
On Error GoTo 0
If sLastUndoStackItem = "Filter" Or sLastUndoStackItem = "Slicer Operation" Then
Set lo = Sheets("Sheet1").Range(sTable).ListObject
For Each sc In ActiveWorkbook.SlicerCaches
On Error Resume Next
sTest = sc.PivotTables(1).Name
On Error GoTo 0
If sTest = sPivot Then
Set lc = lo.ListColumns(sc.SourceName)
If sc.FilterCleared Then
lo.Range.AutoFilter Field:=lc.Index
Else
ReDim vItems(1 To 1)
For Each si In sc.SlicerItems
If si.Selected Then
i = i + 1
ReDim Preserve vItems(1 To i)
vItems(i) = si.Name
End If
Next si
lo.Range.AutoFilter Field:=lc.Index, Criteria1:=vItems, Operator:=xlFilterValues
ReDim vItems(1 To 1)
End If
End If
Next sc
End If
End If
End Sub
使用此代码,我能够 select 来自枢轴 table 切片器的项目,以及常规 table 过滤器。这在英语 excel 版本中完美运行,但在其他语言(如波兰语)中不起作用。这意味着当我 select 来自波兰语 Excel 版本的数据透视 table 切片器的项目时,只有数据透视 table 过滤器,而常规 table 不受影响。我没有从 VBA 中得到任何错误。我正在寻找一种无论语言版本如何都适用的解决方案。
我想问题可能与以下线路有关:
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
我正在使用 Excel 2016.
谁能帮我解决这个问题?
尝试调查一下,找出您的代码的哪一部分没有按预期工作。
Sub Investigate()
Dim Bar As CommandBar
Dim UndoControl As CommandBarControl
Set UndoControl = Application.CommandBars(14).FindControl(ID:=128)
If UndoControl Is Nothing Then
Debug.Print "UndoControl ID 128 not found"
Set UndoControl = Application.CommandBars("Standard").Controls("&Cofnij")
If UndoControl Is Nothing Then
Debug.Print "UndoControl &Cofnij not found"
Exit Sub
End If
End If
If UndoControl.ListCount > 0 Then
Debug.Print UndoControl.List(1)
Else
Debug.Print "List is empty"
End If
End Sub
检查你得到的输出。
在这段代码之后
On Error Resume Next 'in case the undo stack has been wiped or doesn't exist
sLastUndoStackItem = Application.CommandBars(14).FindControl(ID:=128).List(1) 'Standard Commandbar, undo stack
'The above line doesn't seem to work in my version of O365 so we'll use the English language backup
If sLastUndoStackItem = "" Then sLastUndoStackItem = Application.CommandBars("Standard").Controls("&Undo").List(1)
On Error GoTo 0
您应该检查是否找到堆栈项目。因此,如果未发现任何内容,您会收到通知,而不是无声的 “什么都没有发生”.
If sLastUndoStackItem = "" Then
MsgBox "No stack item found."
Exit Sub
End If