微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据所选的切片器来编辑VBA脚本以保存到特定文件夹

如何解决如何根据所选的切片器来编辑VBA脚本以保存到特定文件夹

我已经开发了一个Dashboard,该Dashboard会基于以下两个切片器进行更改:部门和员工。

我的最终目标是使附加的代码在切片器中的所选部门上运行,第二个切片器将选择所有员工,然后该代码将遍历每个员工并打印到PDF以将每个员工保存在其中他们的透视部门文件夹。

文件夹将以部门名称作为文件名称,因此我在考虑某种=运算符的含义。我从另一个用户那里拉了代码

*The below VBA code goes through and selects all and places it in the folder. Is there a way to have 
 the PDF file save depending department or the first slicer. 
 
Private Sub PowerPivotLoopSlicerSimplePrintoPDF()
Dim SC As SlicerCache
Dim SL As SlicerCacheLevel
Dim SI As SlicerItem
Set SC = ActiveWorkbook.SlicerCaches("Slicer_Employee") 'Add slicer name between " "
Set SL = SC.SlicerCacheLevels(1)
'c(ounter) is set to 1,ready to begin
c = 1
'Repeat the a loop until number of prints exceeds number of items in slicer
do while c <= SC.SlicerCacheLevels.Item.Count + 1
'This makes sure that SI is the correct slicer. Needed for corrent file name.
For Each SI In SL.SlicerItems
    If SI.Selected = True Then
    SlicerverdiIndex = c
Exit For
    End If
Next SI

'PRINT CODE
Dim FName           As String
Dim FPath           As String
'Define file path for printed file storage
FPath = "C:\prchica\Desktop\scorecard\PDF files"   'Choose your filepath
FName = SI.sourceName
'Define WHAT to print and how to build file name
 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,Filename:= _
FPath & "\" & FName & ".pdf",Quality:=xlQualityStandard,_
IncludeDocProperties:=False,IgnorePrintAreas:=False,OpenAfterPublish:= _
False,From:=1,To:=1
'PRINT CODE FINISHED
'Sets the slicer to the last item in the list
If SlicerverdiIndex = 1 Then
SlicerverdiIndex = SC.SlicerCacheLevels.Item.Count + 1
End If
SC.VisibleSlicerItemsList = SL.SlicerItems(SlicerverdiIndex - 1).Name
'Adds 1 to the counter,will loop until end of slicer has been reached.
c = c + 1
Loop
End Sub*

解决方法

我需要将其添加到我的代码中

Dim SC As SlicerCache

Set SC = ActiveWorkbook.SlicerCaches("Slicer_Department") 'Add slicer name between " "
Call ParseAndPrintSlicer(SC)

Set SC = ActiveWorkbook.SlicerCaches("Slicer_Emplid") 'Add slicer name between " "
Call ParseAndPrintSlicer(SC)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。