save as PDF

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
Dear all

Below code I am trying to create
Rich (BB code):
Sub PDFSAVEE()

Dim ws As Worksheet
Dim INVname(1 To 3) As Variant

INVname1 = "INV" '''''' i would like to add whatever number after INV. How?
INVname2 = "Reim"
INVname3 = "DN"

For Each ws In Worksheets
    For i = LBound(INVname) To UBound(INVname)
        If ws.Name = ws.Name(i) Then
        ws.ExportAsFixedFormat xlTypePDF, Filename:= _
        "C:\User\Vanda\Desktop\MyPDF.pdf"
        End If
        Next i
Next ws

End Sub
Please kindly guide me what to do one the red line?

I want if the worksheets name "INV" or "Reim" or "DN", save as them in PDF

Thank you in advance
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
This is how I would write that code...

Code:
Sub PDFSAVEE()
    Dim fPath As String
    fPath = "C:\User\Vanda\Desktop\"
    Dim ws As Worksheet
    For Each ws In Worksheets
        Select Case ws.Name
            Case "INV", "Reim", "DN"
                ws.ExportAsFixedFormat xlTypePDF, Filename:=fPath & "MyPDF_" & ws.Name & ".pdf"
            Case Else
                'do nothing
        End Select
    Next ws
End Sub

I am not sure however, what your commented question in the code was asking: "i would like to add whatever number after INV. How?"

Also, I am assuming that you wanted to save each worksheet as a separate PDF.
 
Last edited:
Upvote 0
...and this version of the code (slightly more complex) would essentially do the same as the above code, only it would save the worksheets as a single PDF with multiple pages.

Code:
Sub PDFSAVEE()

    Dim wsStart As Worksheet
    Set wsStart = ActiveSheet

    Dim ws As Worksheet
    Dim wsNonSel As Worksheet
    Dim i As Integer
    i = 0
       
    Application.ScreenUpdating = False
       
    For Each ws In Worksheets
        Select Case ws.Name
            Case "INV", "Reim", "DN"
                If i = 0 Then
                    ws.Select
                Else
                    ws.Select False
                End If
                i = i + 1
            Case Else
                Set wsNonSel = ws
        End Select
    Next ws

    Dim fPath As String
    fPath = "C:\User\Vanda\Desktop\"
    
    If i > 0 Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPath & "MyPDF.pdf"
        wsNonSel.Select
        wsStart.Select
    End If
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Cool. Both code I really need. Thank you very much.

I am not sure however, what your commented question in the code was asking: "i would like to add whatever number after INV. How?"

I meant that my sheets name have not only INV, Reim or DN. They have numeric after.
Ex: INV001, Reim001 or DN001.

So if i cant add it INVname or Case function, the code cannot work either.
Ex: INVname = "INV" & Number or Case "INV" & Number, "Reim" & Number, "DN" & Number

Sorry for my poor English.
 
Upvote 0

Forum statistics

Threads
1,196,357
Messages
6,014,760
Members
441,847
Latest member
Linki

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