Trouble with macro saving worksheets as PDFs

pencekj

New Member
Joined
Jul 16, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
My macro is supposed to go through every worksheet in the workbook and save them as a PDF, but it will only work for 5 worksheets and then errors out (there are 107 total worksheets in the workbook). Any ideas what I'm doing wrong?


Sub Macro1()

Dim ws As Worksheet
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet

For Each ws In ThisWorkbook.Worksheets
ws.Activate

Dim saveLocation As String
saveLocation = "C:\Users\James Pencek\Documents\Invoices" & ActiveSheet.Name & " " & Format(Now(), "MMM. YY") & ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation

Next

starting_ws.Activate

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
The following four character < " | > are legit sheet name characters, but not valid for use in a file name.
Replacing each of them with the underscore character might solve your issue.
Rich (BB code):
Sub Macro1()

    Dim ws As Worksheet
    Dim starting_ws As Worksheet
    Set starting_ws = ActiveSheet

    For Each ws In ThisWorkbook.Worksheets
        ws.Activate

        Dim saveLocation As String
        saveLocation = "C:\Users\James Pencek\Documents\Invoices" & ActiveSheet.Name & " " & Format(Now(), "MMM. YY") & ".pdf"

        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ValidateFileName(saveLocation)

    Next

    starting_ws.Activate
End Sub


VBA Code:
Public Function ValidateFileName(ByVal argFileName As String) As String

    Const UNWANTED As String = "<>"":/\|?*"

    Dim Result As String, i As Long
    Result = argFileName
    For i = 1 To Len(UNWANTED)
        Result = Replace(Result, Mid(UNWANTED, i, 1), "_")
    Next
    ValidateFileName = Result
End Function
 
Upvote 0
i would guess that the sheet name has an invalid charater
The weird thing is that it still only saves the first 5 even if I rearrange the order of the worksheets. Ones that would save won't when I move them to 6th and ones that would error will save when I move them within the first 5. Its so bizarre
 
Upvote 0
Could you provide us with the error code and description? It could tell us more about the possible cause.
 
Upvote 0
Could you provide us with the error code and description? It could tell us more about the possible cause.
Run-time error '5':

Invalid procedure call or argument

and then when I click debug, it highlights this part of code

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation
 
Upvote 0
Try this
VBA Code:
Sub pencekj()

    Dim ws              As Worksheet
    Dim saveLocation    As String

    For Each ws In ThisWorkbook.Worksheets

        saveLocation = "C:\Users\James Pencek\Documents\Invoices" & ws.Name & " " & Format(Now(), "MMM. YY") & ".pdf"
        ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ValidateFileName(saveLocation)
        DoEvents

    Next
End Sub

Public Function ValidateFileName(ByVal argFileName As String) As String

    Const UNWANTED As String = "<>"":/\|?*"

    Dim Result As String, i As Long
    Result = argFileName
    For i = 1 To Len(UNWANTED)
        Result = Replace(Result, Mid(UNWANTED, i, 1), "_")
    Next
    ValidateFileName = Result
End Function
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,689
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top