On PC this VBA runs perfectly, but on Mac, it is showing a printing error. how to correct the script so? Address is "Macintosh HD:Users:myname:Desktop:"
Option Explicit
Sub ExportSheetsToPDFs()
Dim ws As Worksheet
Dim mywsname As String
For Each ws In Worksheets
ws.Select
mywsname = ws.Name
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Users\Accounts\Desktop\" & mywsname & "_" & Left$(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 19) & ".pdf", _
openafterpublish:=False
Next ws
End Sub
Related
I have never really worked with VBA and I am getting stuck on a simple problem.
Essentially I have a workbook with multiple worksheets. I Have a Macro which prints all worksheets to pdf. I want to change the macro to only print specific worksheets
Code sofar:
Sub Print_All_pages()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
Dim Filepath As String
Filepath = Range("Destination").Value & "\Übersicht Bonus - " & Range("Month").Value & " " & Range("Year").Value & " - " & nm & ".pdf"
Range("A1:N35").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Filepath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Next ws
Worksheets("Main Sheet").Activate
End Sub
I hav tried the following which doesn't work
For Each ws in Worksheets
If ws.Name = "Textbausteine Mail" Or ws.Name = "Overzichten verzenden" Or ws.Name = "Übersicht_Januar" Then
'Do nothing
Else
nm = ws.Name
Dim Filepath As String
Filepath = Range("Destination").Value & "\Übersicht Bonus - " & Range("Month").Value & " " & Range("Year").Value & " - " & ws.Name & ".pdf"
Range("A1:N35").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Filepath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Next ws
Worksheets("Main Sheet").Activate
This however throws me the error compilation error Next without for
Here is the workbook setup:
Any help is greatly appreciated
Export Range to PDF
A Quick Fix
Option Explicit
Sub ExportToPDF()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim FilePathLeft As String
With wb.Worksheets("Main Sheet")
FilePathLeft = .Range("Destination").Value & "\Übersicht Bonus - " _
& .Range("Month").Value & " " & .Range("Year").Value & " - "
End With
Dim ws As Worksheet
Dim FilePath As String
For Each ws In wb.Worksheets
Select Case ws.Name
' do NOT export:
Case "Textbausteine Mail", "Overzichten verzenden", "Übersicht_Januar"
' do export:
Case Else
FilePath = FilePathLeft & ws.Name & ".pdf"
ws.Range("A1:N35").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FilePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
End Select
Next ws
End Sub
Very easy VBA, but continuously giving me an error on Excel for Mac. Any leads? Thanks!
Option Explicit
Sub ExportSheetsToPDFs()
Dim ws As Worksheet
Dim mywsname As String
For Each ws In Worksheets
ws.Select
mywsname = ws.Name
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:="C:\" & mywsname & ".pdf", _
openafterpublish:=True
Next ws
End Sub
I'm trying to create separate PDFs for each sheet in a selection of sheets, with a name determined by the sheet name and the contents of one cell.
The code is as follows:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim sh As Object
Dim InvDate As String
With ActiveWorkbook
sPath = .Path & "\"
For Each sh In ActiveWindow.SelectedSheets
InvDate = Format(Range("G9"), "dd-mm-yy")
sFile = sh.Name & " - Invoice - " & InvDate & ".pdf"
sh.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next sh
End With
End Sub
Separate PDFs are created and named correctly, however, the PDF contains all the selected sheets instead of one.
With my experience, i've seen that the ExportAsFixedFormat always exports all selected sheets.
You can come around this by saying sh.Select after your For Each, and then it should works as you describe in the OP.
Edited code:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim sh As Object
Dim InvDate As String
With ActiveWorkbook
sPath = .Path & "\"
For Each sh In ActiveWindow.SelectedSheets
sh.Select '<----- New LINE
InvDate = Format(Range("G9"), "dd-mm-yy")
sFile = sh.Name & " - Invoice - " & InvDate & ".pdf"
sh.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Next sh
End With
End Sub
Hope this helps you achive your goal. :)
So I'm using this macro to save individual excel worksheets as their own PDF when run:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next wks
End With
End Sub
I would just like to add to it to not start from a certain worksheet or to exclude certain worksheets. Any clue how?
You can use Select Case to specify sheetnames to ignore, as shown. Simply replace the "IgnoreSheet1", "IgnoreSheet2" with the actual sheet names you want skipped. It's just a comma delimited list, so add as many as you want.
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
Select Case wks.Name
Case "IgnoreSheet1", "IgnoreSheet2" 'Do nothing
Case Else
'Code here to run on the other sheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Select
Next wks
End With
End Sub
I have a workbook with many worksheets. I would like to save as two-page PDFs, where the first page is Worksheet 1, and the second page is Worksheets 2-x. My code currently only allows me to save individual PDFs for each worksheet in the workbook. I am wondering what to add to it to make it do this. Can anyone share some advice?
Thanks!
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
You are enumerating through the worksheets and doing your save inside that loop. That is why you are getting one PDF per worksheet. Try using just workbook instead of ActiveWorkbook.Worksheets.
Gah. It was staring me in the face the whole time. I amended the code to include a selection, and named the second worksheet ws.Name. Final script looks like this:
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
Sheets(Array("Sheet1", ws.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
Thanks for your help everyone!