创建具有变量名称的新文件夹,并将文件放入具有变量名称的新文件夹中

时间:2018-06-14 20:42:32

标签: save

我正在尝试创建一个运行在切片器列表中的代码,并从生成的数据透视表中创建一个PDF并将其保存在以切片器名称命名的新文件夹中,然后将该文件放在带有切片器的新文件夹中名字和其他一些词。 当我到达MkDir语句时,以及当我到达ActiveSheet.SaveAs时,我收到错误(或者上面有一个注释,我已经尝试过但没有工作的项目)。错误说无法找到Path \ Access,当问题出在ActiveSheet.SaveAs语句时,它是“文档无法保存”

  Sub SelectSlicer()
    'Make sure that you have the sheet open on Excel and it is the active sheet

    'clears filters on all slicers
    SlicerCount = ActiveWorkbook.SlicerCaches.Count
    For X = 1 To SlicerCount
    ActiveWorkbook.SlicerCaches(X).ClearManualFilter
    Next

    'Begin selection
    'loop through each slicer item
    Dim SlItem As SlicerItem, SlDummy As SlicerItem
    Set SlZone = ActiveWorkbook.SlicerCaches("Slicer_Zone")
    'remove below statement if you want to see process on excel document. Makes program run faster as is
    Application.ScreenUpdating = True
    Dim FileFolder As String

     With SlZone


    For Each SlItem In SlZone.SlicerItems
        'show all items to start
        SlZone.ClearManualFilter

        'test each item against iteslf
        For Each SlDummy In SlZone.SlicerItems

            'if the item equals the item in the first loop, then select it
            'otherwise don't show it (thus showing 1 at a time between the nested loops
            If SlItem.Name = SlDummy.Name Then SlDummy.Selected = True Else: SlDummy.Selected = False

                'Selection of Pivot Table and export as PDF to the File I:\LPP\LPP FP&A\Reporting\Headcount\2018\Zone Reports

        Next SlDummy

            'Create folder as Slicer Name. Save Filename as Slicer Name HC Report
            'This will make the file name read as Slicer name and HC Report
            Dim FileName As String
                FileName = SlItem.Name & " " & "HC Report"
            'This will be used as name for the new folder.
            Dim newFolder As String
                newFolder = SlItem.Name
            'Filename + extension
            Dim sFileName As String
                sFileName = FileName + ".pdf"
            'This is the path YOU HAVE TO CHANGE THE DATE FOLDER AND CREATE THAT BEFORE YOU RUN THE CODE--The Desired folder path
            Dim sFolder As String
                sFolder = "I:\LPP\LPP FP&A\Reporting\Headcount\2018\Zone Reports\07.2018\newFolder\"
            'This is the path combined with sMonth to make the final path
            Dim individualFolders() As String
                'Split the folder path into individual folder names
                individualFolders = Split(sFolder, "\")
                'Loop through each individual folder name
                For Each arrayElement In individualFolders
                'Build string of folder path
                    Dim tempFolderPath As String

                        tempFolderPath = tempFolderPath & arrayElement & "\"
                        'If folder does not exist, then create it
                        If Dir(tempFolderPath, vbDirectory) = "" Then
                            MkDir tempFolderPath
                        End If
                Next arrayElement

                    'Sheets("HC Report").ExportAsFixedFormat Type:=xlTypePDF, FileName:=sFolder & sFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
                    ActiveSheet.SaveAs sFolder & sFileName, _
                    Type:=xlTypePDF

    Next SlItem

   End With

End Sub

0 个答案:

没有答案