date as filename vba

twl2009

Board Regular
Joined
Jan 7, 2016
Messages
247
Im using this line to save a worksheet as a workbook. I want to use a date from the sheet as part of the filename. Vba doesnt like it. Im pretty sure I need to use 'Format..."dd-mm-yy"' around or within 'sh.range("q3")'
But whatever ive tried is giving an error.
Any guidance gratefully received. Thanks


<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}</style>ActiveWorkbook.SaveAs FileName:="/Users/User/Desktop/" & sh.Name & sh.Range("Q3")
 
Code:
For Each sh In ThisWorkbook.Sheets
    If sh.Name <> "Crew Database" And sh.Name <> "Labour Week" And sh.Name <> "Timesheet" Then
        sh.Copy
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="/Users/User/Desktop/" & sh.Name & " " & Format(Range("Q3"), "dd-mm-yy") _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
        ActiveWorkbook.Close
     End If
Next

What your code is doing is looping through ALL Worksheets in your Workbook and
If name of Worksheet is NOT "Crew Database", "Labour Week" or "Timesheet" Then
Saving that worksheet with name "/Users/User/Desktop/" & sh.Name & " " & Format(Range("Q3"), "dd-mm-yy"
then goes to next worksheet and repeats

You should be able to verify in debug mode (I'm a bit confused as to why the ActiveWorkbook.Close line doesn't cause mayhem - probably you select don't save and the code carries on?
SO. It overwrites the pdf each time and all you will get is a pdf of the LAST Worksheet who's name is NOT "Crew Database", "Labour Week" or "Timesheet"

Does that make sense?

If you want a specific Worksheet or ALL the worksheets on your pdf you will need a different code
 
Last edited:
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Ah. That makes sense, what im trying to achieve is for all sheets, apart from the 3 mentioned to be saved as individual pdfs. Is that possible. ??
 
Upvote 0
Sorry. I was talking nonsense. What your Export line does is Export as pdf the current ACTIVATED sheet. Your code was looping through the sheets but NOT activating them!

Code:
Dim sh As Worksheet
For Each sh In ThisWorkbook.Sheets
    sh.Activate
    If sh.Name <> "Crew Database" And sh.Name <> "Labour Week" And sh.Name <> "Timesheet" Then
'        sh.Copy
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="\Users\User\Desktop/" & sh.Name & " " & Format(Range("Q3"), "dd-mm-yy") _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
'        ActiveWorkbook.Close
     End If
Next

Also - Your "/" should be "A backslash**" ** Doesn't display the backslash!
And the sh.Copy was creating new copies of the Worksheet which was creating multiple workbooks with one worksheet in it but the sheet number going up by 1 each time
 
Last edited:
Upvote 0
Thanks Stiaurt. Forgive me my ignorance, but do you think there is a way of downloading the sheets as individual pdfs??
 
Upvote 0
Not sure what you mean by "download"

The code I posted loops through all your sheets, saves as pdf each one (Except for those 3). Only caveat to note is you need to put the date in Q3 on EVERY sheet you want to save as pdf
 
Upvote 0
Hey twl2009

sorry dint see your post... please try to add the below and it should work...sorry saw your post now...

Dim Path As String
Dim filename As String
Path = "INPUT YOUR PATH HERE"
filename = Range("Q3")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
Path & filename & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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