Macro to create a unique folder name, save file, and print a PDF based on cell value

banderson7563

New Member
Joined
Feb 2, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I currently use a spreadsheet to create all my quotes for my customers. Overall I have it pretty clean and quick, but one thing that can save me time is being able to automate creating a folder, saving the file, and printing a PDF to email to the customer. All formats are the same for the quote, so the names are being pulled in from the same spot. My goal is for this macro to:
1. create a new folder in C:\Users\myname\My Files\Quotes - the name for the this file would pull from two spots on the sheet and be "B3 - A19". an example of what this would look like would be "ABC Company - XYZ System"
2. Save the workbook in this newly created folder as "Quote - B3" so in the example above it would be "Quote - ABC Company"
3. Create a PDF saved in the newly created folder with the same name as the workbook - "Quote - B3" so in the example it would be "Quote - ABC Company"

Any help or ideas would be great! Thanks in advance
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi and welcome to MrExcel.

Try this:
VBA Code:
Sub macro()
  Dim d1 As String, d2 As String, sPath As String
  Dim wb As Workbook
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  With Sheets("Sheet1")
    d1 = .Range("B3")
    d2 = .Range("A19")
  End With
  
  sPath = Environ$("userprofile") & "\My Files"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes\" & d1 & " - " & d2
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  
  Sheets.Copy
  Set wb = ActiveWorkbook
  wb.SaveAs sPath & "\" & "Quote - " & d1, FileFormat:=xlOpenXMLWorkbook
  wb.ExportAsFixedFormat xlTypePDF, sPath & "\" & "Quote - " & d1 & ".pdf", _
      xlQualityStandard, True, False, OpenAfterPublish:=False
  wb.Close False
End Sub
 
Upvote 0
Hi and welcome to MrExcel.

Try this:
VBA Code:
Sub macro()
  Dim d1 As String, d2 As String, sPath As String
  Dim wb As Workbook
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  With Sheets("Sheet1")
    d1 = .Range("B3")
    d2 = .Range("A19")
  End With
 
  sPath = Environ$("userprofile") & "\My Files"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes\" & d1 & " - " & d2
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
 
  Sheets.Copy
  Set wb = ActiveWorkbook
  wb.SaveAs sPath & "\" & "Quote - " & d1, FileFormat:=xlOpenXMLWorkbook
  wb.ExportAsFixedFormat xlTypePDF, sPath & "\" & "Quote - " & d1 & ".pdf", _
      xlQualityStandard, True, False, OpenAfterPublish:=False
  wb.Close False
End Sub
You are amazing! One thing I need adjusted. Currently this make a multi page PDF with every tab I have. If I only want the PDF to be the 3rd tab which is titled "Customer Quote" what do I need to change?
 
Upvote 0
You are amazing! One thing I need adjusted. Currently this make a multi page PDF with every tab I have. If I only want the PDF to be the 3rd tab which is titled "Customer Quote" what do I need to change?
@DanteAmor one more thing I just ran into while testing this out. Can I put in errors or something along those lines that would put a stop to this if the Folder and File name already exist? Sorry I did not think of that ahead of time. I just want to make sure I don't accidently overwrite a folder or file.
 
Upvote 0
If I only want the PDF to be the 3rd tab which is titled "Customer Quote"
Only that sheet for the pdf.

I just want to make sure I don't accidently overwrite a folder or file.
Only the file can be overwritten. I put a warning in case the file already exists.

VBA Code:
Sub macro()
  Dim d1 As String, d2 As String, sPath As String, sName As String
  Dim wb As Workbook
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  With Sheets("Sheet1")
    d1 = .Range("B3")
    d2 = .Range("A19")
  End With
  
  sPath = Environ$("userprofile") & "\My Files"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes\" & d1 & " - " & d2
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  
  sName = sPath & "\" & "Quote - " & d1
  If Dir(sName & "*") <> "" Then
    If MsgBox("File name already exist: " & vbCr & _
      sName & vbCr & "Overwrite?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
  End If
  Sheets.Copy
  Set wb = ActiveWorkbook
  wb.SaveAs sName, FileFormat:=xlOpenXMLWorkbook
  wb.Sheets("Customer Quote").ExportAsFixedFormat xlTypePDF, sName & ".pdf", xlQualityStandard, True, False, OpenAfterPublish:=False
  wb.Close False
End Sub
 
Upvote 0
Solution
@DanteAmor one more thing I just ran into while testing this out. Can I put in errors or something along those lines that would put a stop to this if the Folder and File name already exist? Sorry I did not think of that ahead of time. I just want to make sure I don't accidently overwrite a folder or file.

Only that sheet for the pdf.


Only the file can be overwritten. I put a warning in case the file already exists.

VBA Code:
Sub macro()
  Dim d1 As String, d2 As String, sPath As String, sName As String
  Dim wb As Workbook
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  With Sheets("Sheet1")
    d1 = .Range("B3")
    d2 = .Range("A19")
  End With
 
  sPath = Environ$("userprofile") & "\My Files"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes"
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
  sPath = Environ$("userprofile") & "\My Files\Quotes\" & d1 & " - " & d2
  If Dir(sPath, vbDirectory) = "" Then MkDir sPath
 
  sName = sPath & "\" & "Quote - " & d1
  If Dir(sName & "*") <> "" Then
    If MsgBox("File name already exist: " & vbCr & _
      sName & vbCr & "Overwrite?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
  End If
  Sheets.Copy
  Set wb = ActiveWorkbook
  wb.SaveAs sName, FileFormat:=xlOpenXMLWorkbook
  wb.Sheets("Customer Quote").ExportAsFixedFormat xlTypePDF, sName & ".pdf", xlQualityStandard, True, False, OpenAfterPublish:=False
  wb.Close False
End Sub
This works per Thanks again!
 
Upvote 0
@DanteAmor I feel bad for keeping you coming back to this. Thank you for all the help so far, it's been amazing! One last thing I just ran into - when the new workbook is made and saved is there a way for the code to now go with that new workbook. I had to make an adjustment on a quote, and when I ran the code, it went back to my original document rather than the newly saved one.

For example - my macro is saved in a file titled - "Customer Quote Template". Once my info is entered for the new customer I run the macro and my new file is saved as "Quote - ABC Company". I needed to make a small change on my "Quote - ABC Company" spreadsheet and when I click the button to run the macro it reopened the "Customer Quote Template" and ran from there without the change I made.

Is there a way to fix this?
 
Upvote 0
I do not understand what do you need. But try the following to see if it helps.
Change this line:

wb.SaveAs sName, FileFormat:=xlOpenXMLWorkbook

For this:

ThisWorkbook.SaveCopyAs sName
 
Upvote 0
I do not understand what do you need. But try the following to see if it helps.
Change this line:

wb.SaveAs sName, FileFormat:=xlOpenXMLWorkbook

For this:

ThisWorkbook.SaveCopyAs sName
I am wanting the Macro to be in the newly saved file and not in the original template file. This does not save the new file as an excel spreadsheet.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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