excel - 如何根据所选的切片器编辑 VBA 脚本以保存到特定文件夹
问题描述
我开发了一个仪表板,它基于 2 个切片器进行更改:部门和员工。
我的最终目标是附加代码在切片器中的选定部门上运行,第二个切片器将选择所有员工,然后代码将遍历每个员工并打印到 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)
推荐阅读
- javascript - async.waterfall 回调在哪里定义
- android - adb 闪烁“emulator-5554 [DISCONNECTED]”
- c# - 带有 itextsharp 的简单目录
- c# - 我如何修复我的初学者 if 和 else 代码?
- ios - 为什么我的自动调整表格视图单元格不起作用,它取决于故事板中标签的文本大小?
- python - 在同一程序实例中执行新命令之前,有没有办法等待命令执行
- dbeaver - 如何在 DBeaver Database Navigator 中按字母顺序对数据库进行排序
- python - 如何从匹配的表达式中找到特定的单词?
- java - 如何向多个收件人发送邮件
- javascript - 我可以在“index.js”文件中的另一个 API 调用(router.post)中进行 API 调用(router.post)吗?