时间线切片器Excel 2013 VBA
希望有人能帮忙 我试图调用vba中的时间线切片器值,以便控制其他切片器。我已经从细胞管理它,但我想知道它是否可以由我的主切片机控制时间线切片器Excel 2013 VBA,vba,excel,Vba,Excel,希望有人能帮忙 我试图调用vba中的时间线切片器值,以便控制其他切片器。我已经从细胞管理它,但我想知道它是否可以由我的主切片机控制 Sub Slicer_Time_Change() Dim startDate As Date, endDate As Date startDate = Range("A1") 'Set slicer 1 start date as slicer selection instead endDate = Range("B1") 'Set slicer 1 En
Sub Slicer_Time_Change()
Dim startDate As Date, endDate As Date
startDate = Range("A1") 'Set slicer 1 start date as slicer selection instead
endDate = Range("B1") 'Set slicer 1 End date as slicer selection instead
ActiveWorkbook.SlicerCaches("NativeTimeline_Date1").TimelineState. _
SetFilterDateRange startDate, endDate
ActiveWorkbook.SlicerCaches )("NativeTimeline_Date2").TimelineState. _
SetFilterDateRange startDate, endDate
端接头
非常感谢 我自己来回答这个问题
Sub Slicer_Time_Change()
'Gets data from master slicer selection
Set cache = ActiveWorkbook.SlicerCaches("NativeTimeline_Date1")
'Puts into cell
Cells(1, 1) = cache.TimelineState.startDate
Cells(1, 2) = cache.TimelineState.endDate
Dim startDate As Date, endDate As Date
startDate = Range("A1")
endDate = Range("B1")
'Takes data from cell and controls other slicers with date range
ActiveWorkbook.SlicerCaches("NativeTimeline_Date2").TimelineState. _
SetFilterDateRange startDate, endDate
End Sub
我自己来回答这个问题
Sub Slicer_Time_Change()
'Gets data from master slicer selection
Set cache = ActiveWorkbook.SlicerCaches("NativeTimeline_Date1")
'Puts into cell
Cells(1, 1) = cache.TimelineState.startDate
Cells(1, 2) = cache.TimelineState.endDate
Dim startDate As Date, endDate As Date
startDate = Range("A1")
endDate = Range("B1")
'Takes data from cell and controls other slicers with date range
ActiveWorkbook.SlicerCaches("NativeTimeline_Date2").TimelineState. _
SetFilterDateRange startDate, endDate
End Sub
我也有同样的问题。你的回答很有帮助,但我需要在主切片器更改时触发它。不幸的是,切片器没有事件,但它们影响的数据透视表有。下面的代码将在主切片器发生更改时更新所有其他时间线切片器,而主切片器又会更改其数据透视表,然后可以使用数据透视表触发对所有其他切片器的更改
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
' Description:Update Timeline Slicers from Master Slicer
' Inputs: Sh PivotTable's worksheet
' Target PivotTable being changed/updated
' Outputs: *None
' Requisites: *None
' Example: *None - This is an event handler found in ThisWorkbook module
' Date Ini Modification
' 10/19/16 CWH Initial Development
' Declarations
Const cRoutine As String = "Workbook_SheetPivotTableUpdate"
Dim oSlicer As SlicerCache 'Current Slicer
Const cSlicer As Long = 1 'Master Slicer
Dim dStartDate As Date 'Start Date
Dim dEndDate As Date 'End Date
Dim bCleared As Boolean 'Filter Cleared Flag
Dim bEvents As Boolean 'Events Enabled Flag
' Error Handling Initialization
On Error GoTo ErrHandler
' Prevent cascading events
bEvents = Application.EnableEvents
Application.EnableEvents = False
' Get Master Slicer's dates
Set oSlicer = ThisWorkbook.SlicerCaches(cSlicer)
bCleared = oSlicer.FilterCleared
If Not bCleared Then
With oSlicer.TimelineState
dStartDate = .FilterValue1
dEndDate = .FilterValue2
End With
End If
' Set All other Timeline Slicer Dates
For Each oSlicer In ThisWorkbook.SlicerCaches
If oSlicer.SlicerCacheType = xlTimeline And _
oSlicer.Index <> cSlicer Then
If bCleared Then _
oSlicer.ClearAllFilters Else _
oSlicer.TimelineState.SetFilterDateRange _
StartDate:=dStartDate, EndDate:=dEndDate
End If
Next
ErrHandler:
Select Case Err.Number
Case Is = 0: 'Do nothing
Case Is = 9: 'Do Nothing Master Slicer Missing
Case Else:
Select Case MsgBox(Prompt:=Err.Description, _
Buttons:=vbAbortRetryIgnore, _
Title:=cRoutine, _
HelpFile:=Err.HelpFile, _
Context:=Err.HelpContext)
Case Is = vbAbort: Stop: Resume 'Debug mode - Trace
Case Is = vbRetry: Resume 'Try again
Case Is = vbIgnore: 'End routine
End Select
End Select
' Clean up: Resume responding to events
Application.EnableEvents = bEvents
End Sub
Private子工作簿\u表数据透视表更新(ByVal Sh作为对象,ByVal Target作为数据透视表)
'说明:从主切片器更新时间线切片器
'输入:Sh数据透视表的工作表
'正在更改/更新的目标数据透视表
“产出:*无
“必备品:*无
'示例:*无-这是在此工作簿模块中找到的事件处理程序
'日期Ini修改
2016年10月19日CWH初步开发
"宣言",
Const crudtine As String=“工作簿\数据透视表更新”
将oSlicer设置为切片器缓存的当前切片器
常量C切片器长度=1'主切片器
Dim dStartDate作为日期的开始日期
Dim dEndDate作为“结束日期”
Dim B清除为布尔“过滤器清除标志”
Dim bEvents As Boolean“事件启用标志”
'错误处理初始化
关于错误转到错误处理程序
“防止级联事件
bEvents=Application.EnableEvents
Application.EnableEvents=False
'获取切片师的日期
设置oSlicer=ThisWorkbook.SlicerCaches(cSlicer)
b清除=oSlicer.filter清除
如果不清楚的话
使用oSlicer.TimelineState
dStartDate=.FilterValue 1
DenDate=.FilterValue 2
以
如果结束
'设置所有其他时间线切片器日期
对于此工作簿中的每个oSlicer.SlicerCaches
如果oSlicer.SlicerCacheType=xlTimeline和_
oSlicer.索引cSlicer然后
如果我当时明白了_
oSlicer.ClearAllFilters其他_
oSlicer.TimelineState.setFilterDaterRange_
StartDate:=dStartDate,EndDate:=dEndDate
如果结束
下一个
错误处理程序:
选择案例错误编号
Case Is=0:'什么都不做
案例Is=9:'不执行任何操作主切片器丢失
其他情况:
选择案例MsgBox(提示:=Err.Description_
按钮:=vbAbortRetryIgnore_
标题:=克劳丁_
帮助文件:=Err.HelpFile_
上下文:=Err.HelpContext)
案例Is=vbAbort:Stop:Resume'调试模式-跟踪
Case Is=vbRetry:Resume'重试
Case Is=vbIgnore:'结束例程
结束选择
结束选择
'清理:恢复对事件的响应
Application.EnableEvents=bEvents
端接头
我也有同样的问题。你的回答很有帮助,但我需要在主切片器更改时触发它。不幸的是,切片器没有事件,但它们影响的数据透视表有。下面的代码将在主切片器发生更改时更新所有其他时间线切片器,而主切片器又会更改其数据透视表,然后可以使用数据透视表触发对所有其他切片器的更改
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
' Description:Update Timeline Slicers from Master Slicer
' Inputs: Sh PivotTable's worksheet
' Target PivotTable being changed/updated
' Outputs: *None
' Requisites: *None
' Example: *None - This is an event handler found in ThisWorkbook module
' Date Ini Modification
' 10/19/16 CWH Initial Development
' Declarations
Const cRoutine As String = "Workbook_SheetPivotTableUpdate"
Dim oSlicer As SlicerCache 'Current Slicer
Const cSlicer As Long = 1 'Master Slicer
Dim dStartDate As Date 'Start Date
Dim dEndDate As Date 'End Date
Dim bCleared As Boolean 'Filter Cleared Flag
Dim bEvents As Boolean 'Events Enabled Flag
' Error Handling Initialization
On Error GoTo ErrHandler
' Prevent cascading events
bEvents = Application.EnableEvents
Application.EnableEvents = False
' Get Master Slicer's dates
Set oSlicer = ThisWorkbook.SlicerCaches(cSlicer)
bCleared = oSlicer.FilterCleared
If Not bCleared Then
With oSlicer.TimelineState
dStartDate = .FilterValue1
dEndDate = .FilterValue2
End With
End If
' Set All other Timeline Slicer Dates
For Each oSlicer In ThisWorkbook.SlicerCaches
If oSlicer.SlicerCacheType = xlTimeline And _
oSlicer.Index <> cSlicer Then
If bCleared Then _
oSlicer.ClearAllFilters Else _
oSlicer.TimelineState.SetFilterDateRange _
StartDate:=dStartDate, EndDate:=dEndDate
End If
Next
ErrHandler:
Select Case Err.Number
Case Is = 0: 'Do nothing
Case Is = 9: 'Do Nothing Master Slicer Missing
Case Else:
Select Case MsgBox(Prompt:=Err.Description, _
Buttons:=vbAbortRetryIgnore, _
Title:=cRoutine, _
HelpFile:=Err.HelpFile, _
Context:=Err.HelpContext)
Case Is = vbAbort: Stop: Resume 'Debug mode - Trace
Case Is = vbRetry: Resume 'Try again
Case Is = vbIgnore: 'End routine
End Select
End Select
' Clean up: Resume responding to events
Application.EnableEvents = bEvents
End Sub
Private子工作簿\u表数据透视表更新(ByVal Sh作为对象,ByVal Target作为数据透视表)
'说明:从主切片器更新时间线切片器
'输入:Sh数据透视表的工作表
'正在更改/更新的目标数据透视表
“产出:*无
“必备品:*无
'示例:*无-这是在此工作簿模块中找到的事件处理程序
'日期Ini修改
2016年10月19日CWH初步开发
"宣言",
Const crudtine As String=“工作簿\数据透视表更新”
将oSlicer设置为切片器缓存的当前切片器
常量C切片器长度=1'主切片器
Dim dStartDate作为日期的开始日期
Dim dEndDate作为“结束日期”
Dim B清除为布尔“过滤器清除标志”
Dim bEvents As Boolean“事件启用标志”
'错误处理初始化
关于错误转到错误处理程序
“防止级联事件
bEvents=Application.EnableEvents
Application.EnableEvents=False
'获取切片师的日期
设置oSlicer=ThisWorkbook.SlicerCaches(cSlicer)
b清除=oSlicer.filter清除
如果不清楚的话
使用oSlicer.TimelineState
dStartDate=.FilterValue 1
DenDate=.FilterValue 2
以
如果结束
'设置所有其他时间线切片器日期
对于此工作簿中的每个oSlicer.SlicerCaches
如果oSlicer.SlicerCacheType=xlTimeline和_
oSlicer.索引cSlicer然后
如果我当时明白了_
oSlicer.ClearAllFilters其他_
oSlicer.TimelineState.setFilterDaterRange_
StartDate:=dStartDate,EndDate:=dEndDate
如果结束
下一个
错误处理程序:
选择案例错误编号
Case Is=0:'什么都不做
案例Is=9:'不执行任何操作主切片器丢失
其他情况:
选择案例MsgBox(提示:=Err.Description_
按钮:=vbAbortRetryIgnore_
标题:=克劳丁_
帮助文件:=Err.HelpFile_
上下文:=Err.HelpContext)
案例Is=vbAbort:Stop:Resume