How to create a second folder with todays date and saving a file using a cell value.

ma1sk1y

New Member
Joined
Sep 12, 2013
Messages
13
Hi, Im trying to create a directory and then export a pdf into this directory. What I have currently works but I would like to avoid overwriting older files that may have same filename. I would like to add another folder with todays date.

Everything I tried gives me errors.
Thanks in advance!
Small portion of code

I want to end up having C:\LabReports\12Sep2013 "filename from cell c8 in current sheet"

Code:
On Error Resume Next
MkDir "C:\LabReports\"
On Error GoTo 0
Sheets("Single Well Data Input").Select
If ActiveSheet.Range("B8").Value = "" Then GoTo error
    Range("B8:Z8").Select
    Selection.Copy
    Range("Table14[Well Name]").Select
    ActiveSheet.Paste
    Sheets("Water Analysis").Select
    Application.CutCopyMode = False
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\LabReports\" & Range("c8").Value _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I have not replicated your code but I have included the folder processing parts.

Basically, you need three things: Part 1 on the path, part 2 of the path and the file name.
I made Part 1 = "C:\LabReports"; Part 2 = Today's Date (automatic); Part 3 = "Sample.pdf".
I then joined parts 1 and 2 together to make the file path.

All three need to be joined together for the line:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myPath & "/" & myFile ...

You can make any or all cell contents or hard-coded or whatever.

First the macro checks the base folder and creates it if it does not exist. Then it checks the second level folder and creates it if it does not exist. (I swapped the date format round because this will come out in date order when sorted - personnel preference YMMV.)

Code:
Sub myTest()
    Dim myFolder As String
    Dim myPath As String
    Dim myFile As String
    myFolder = "C:\LabReports"
        If Dir(myFolder, vbDirectory) = vbNullString Then
        MkDir myFolder
    End If
    myPath = myFolder & "/" & Format(Date, "yyyy-mm-dd")
    If Dir(myPath, vbDirectory) = vbNullString Then
        MkDir myPath
    End If
    myFile = "Sample.pdf"
    
    
    ' Use ...    Filename:= myPath & "/" & myFile
    
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,484
Members
449,455
Latest member
jesski

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