What This VBA Macro Code Does
The business world has increasingly become more reliant on mobile computing with devices such as tablets and smart phones becoming mainstream. This, in turn, has provided an elevated demand for analysts to turn spreadsheets into PDF documents so management can view your reports on the go.
Below is a simple VBA macro that will allow you to quickly turn your selected worksheet(s) into a PDF file in a snap. The code is written to save your document in the same folder as the Excel file currently resides. If you need to make modifications, hopefully, you will be able to follow along with my code comments and customize the code to your specific needs.
'PURPOSE: Generate A PDF Document With Selected Worksheet(s)
'NOTES: PDF Will Be Saved To Same Folder As Excel File
Dim CurrentFolder As String
Dim FileName As String
Dim myPath As String
Dim UniqueName As Boolean
UniqueName = False
'Store Information About Excel File
myPath = ActiveWorkbook.FullName
CurrentFolder = ActiveWorkbook.Path & "\"
FileName = Mid(myPath, InStrRev(myPath, "\") + 1, _
InStrRev(myPath, ".") - InStrRev(myPath, "\") - 1)
'Does File Already Exist?
Do While UniqueName = False
DirFile = CurrentFolder & FileName & ".pdf"
If Len(Dir(DirFile)) <> 0 Then
UserAnswer = MsgBox("File Already Exists! Click " & _
"[Yes] to override. Click [No] to Rename.", vbYesNoCancel)
If UserAnswer = vbYes Then
UniqueName = True
ElseIf UserAnswer = vbNo Then
'Retrieve New File Name
FileName = Application.InputBox("Provide New File Name " & _
"(will ask again if you provide an invalid file name)", , _
'Exit if User Wants To
If FileName = "False" Or FileName = "" Then Exit Sub
Loop While ValidFileName(FileName) = False
Exit Sub 'Cancel
UniqueName = True
'Save As PDF Document
On Error GoTo ProblemSaving
FileName:=CurrentFolder & FileName & ".pdf", _
On Error GoTo 0
'Disable Page Breaks
ActiveSheet.DisplayPageBreaks = False
'Confirm Save To User
FolderName = Mid(.Path, InStrRev(.Path, "\") + 1, Len(.Path) - InStrRev(.Path, "\"))
MsgBox "PDF Saved in the Folder: " & FolderName
MsgBox "There was a problem saving your PDF. This is most commonly" & _
" caused by the original PDF file already being open."
Function To Validate Save File Name
Below is a function that you will need to paste in along with the above macro. The VBA function provides a way of testing any file name your users provide to save the PDF document as.
Function ValidFileName(FileName As String) As Boolean
'PURPOSE: Determine If A Given Excel File Name Is Valid
Dim TempPath As String
Dim wb As Workbook
'Determine Folder Where Temporary Files Are Stored
TempPath = Environ("TEMP")
'Create a Temporary XLS file (XLS in case there are macros)
On Error GoTo InvalidFileName
Set wb = ActiveWorkbook.SaveAs(ActiveWorkbook.TempPath & _
"\" & FileName & ".xls", xlExcel8)
On Error Resume Next
'Delete Temp File
'File Name is Valid
ValidFileName = True
'File Name is Invalid
ValidFileName = False
Same Macro Functionality For Other Office Applications
Upon request, I have made similar macros for other Office Applications you may use on a regular basis to convert their files into PDF documents. The links to those specific posts are as follows:
How Do I Modify This To Fit My Specific Needs?
Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it!
I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).
I wish you the best of luck and I hope this tutorial gets you heading in the right direction!