我是VBA的新手,并且在此站点上尝试了一些其他解决方案,但是没有运气。我在一个excel文件中有一个仪表板,该仪表板链接到多个表-我想更改位置切片器并为每个站点打印仪表板。 在单独的文件中,我有一些VBA代码可以循环遍历所选活动工作簿中的位置切片器 我想我已经得出结论,因为我正在使用PowerPivot提取数据,因此我需要使用slicercachelevel。
我现在得到的错误是运行时错误1004应用程序定义的错误或对象定义的错误。
这是我到目前为止所拥有的:
Sub DashboardCreate()
Dim wb As String
Dim sh As Worksheet
Dim fname As String
Dim location As String
Dim sI As SlicerItem, sI2 As SlicerItem, sC As SlicerCache
Dim index As Integer
location = "T:\Sarah\Weekly Sales Reports\"
Set sC = ActiveWorkbook.SlicerCaches("Slicer_LocationName7")
'loop through
With sC
For Each sI In sC.SlicerCacheLevels(1).SlicerItems
sC.ClearManualFilter
For Each sI2 In sC.SlicerCacheLevels(1).SlicerItems
*error appears on this next line*
If sI.Name = sI2.Name Then sI2.Selected = True Else: sI2.Selected = False
Next
Debug.Print sI.Name
fname = sh.Range("B11").Value
sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:=location & fname & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Next
End With
End Sub
任何帮助将不胜感激。
答案 0 :(得分:0)
这篇文章可能会有所帮助,因为处理powerpivot slicer需要不同的方法
答案 1 :(得分:0)
此代码中的循环可在电源枢轴上工作,我已编辑了一些代码以适合您的问题。您可以使用代码调整文件名,打印设置等。
'This VBA will loop through your Power Pivot slicer and print the results to PDF.
'To get it working change slicer name and storage location in below VBA.
Private Sub PowerPivotLoopSlicerPrintPDF()
Dim SC As SlicerCache
Dim SL As SlicerCacheLevel
Dim SI As SlicerItem
Set SC = ActiveWorkbook.SlicerCaches("Slicer_LocationName7") '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 = "T:\Sarah\Weekly Sales Reports\" '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:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
'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