Hello. I have this code wich works fine for every sheet, I'd like to make it to save all sheets with one click only.
I need all the sheets in the workbook to be saved separately with the sheet name and the year given in a cell like the code shows. It works like a charm but I have to go on page by page and run the macro as it is.
Is it possible to make it loop the sheets and save them all with just one time click?
Best regards,
eLy
I need all the sheets in the workbook to be saved separately with the sheet name and the year given in a cell like the code shows. It works like a charm but I have to go on page by page and run the macro as it is.
Is it possible to make it loop the sheets and save them all with just one time click?
Code:
Here's the code I have:
Private Sub Save_As_PDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Worksheets("Alterações").Range("C17").Value _
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, "", "")
strName = Replace(strName, "", "")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected ( I tried to edit this part with another code but it just wont work or remoes the date option)
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Best regards,
eLy