Save as PDF

BrendanDixon

Board Regular
Joined
Mar 7, 2010
Messages
174
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi All,

I have a sheet where I click on a button and it will save the sheet as a PDF file with a predetermined name. The Problem I have is that I sometimes might do multiple sheets on the same day so when I click the button it will overwrite the first form. I am wonderinf is someone could tell me how to edit my code to append the filename with a 1 or 2 or 3 etc. This is my code below.

Code:
ActiveWindow.SelectedSheets.PrintOut Copies:=(Sheets("Input").Range("C16").Value), Collate:=True
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=(Sheets("Input").Range("C17").Value & "\" & Sheets("Input").Range("C8").Value & " " & Sheets("Input").Range("D8").Value & " " & Sheets("Input").Range("E8").Value & " " & Sheets("Input").Range("C4").Value & ".pdf"), Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Here is a little snippet of code you should be able to incorporate into your macro. It checks to see if the file name already exists, and if it does, it adds a counter to the end (1,2,3,...etc until it finds the first unused one).
Code:
    Dim myFileName As String
    Dim myFileNamePrefix As String
    Dim i As Long
 
'   Build file name here
    myFileName = "G:\C\Temp\Book1.pdf"
    myFileNamePrefix = Left(myFileName, Len(myFileName) - 4)
    
'   Loop until you find an unused filename
    i = 0
    Do
        If Not (Dir(myFileName) = vbNullString) Then
            i = i + 1
            myFileName = myFileNamePrefix & i & ".pdf"
        Else
            Exit Do
        End If
    Loop
    
    MsgBox myFileName
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

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