VBA to save active file wtih file name and date

General Ledger

Active Member
Joined
Dec 31, 2007
Messages
460
Dear All,

I am trying to create code that will take the name of the active file (csv, txt, xls, xlsx), concatenate the current date and time, and open Save As dialog box with a default folder.

The below code does all that except finding the name of the active file. How can I pull just the name of the file from the active file?

Code:
Sub SaveAsDialog()
    Dim strFolder As String
    FileName = "Accounts Payable " & Format(Now, "yyyy-mm-dd hh mm") & ".xlsx"
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = "P:\Audit 2011\" & FileName
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
        .Execute
    End With
End Sub

Thank you,

GL
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I updated my code based on the response from VoG.

My problem is the FullName returns the name of the file with the file extention (.xls) How can I strip off the file extention from the file name?

Code:
Sub SaveAsDialog()
    Dim strFolder As String
    FileName = ActiveWorkbook.FullName & " " & Format(Now, "yyyy-mm-dd hh mm") & ".xlsx"
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = "P:\Audit 2011\" & FileName
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
        .Execute
    End With
 
End Sub
 
Upvote 0
Try like this

Code:
Sub SaveAsDialog()
    Dim strFolder As String
    Dim i As Long
    i = InStr(ActiveWorkbook.Name, ".xlsx")
    Filename = Left(ActiveWorkbook.Name, i - 1) & " " & Format(Now, "yyyy-mm-dd hh mm") & ".xlsx"
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = "P:\Audit 2011\" & Filename
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
        .Execute
    End With
 
End Sub
 
Upvote 0
VoG, you did it. Here is my final version. Thanks so much.

Code:
Sub SaveAsDialog()
    Dim strFolder As String
    Dim i As Long
 
    'Find the position of the period in the file name
    i = InStr(ActiveWorkbook.Name, ".")
 
    'Create a default file name by concatenating the file name without the extention _
        plus the current date and time, and plus the xlsx extention
    FileName = Left(ActiveWorkbook.Name, i - 1) & " " & Format(Now, "yyyy-mm-dd hh mm") & ".xlsx"
 
    'Open Save As dialog to a default folder with default file name
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = "P:\Audit 2011\" & FileName
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
        .Execute
    End With    
End Sub
 
Upvote 0
ok my saving works fine as the code shown... but my issue is the File Save as dialog box comes up and then I manually have to click "Save" and then it will save the excel document in the relevant folder I specified. I need my code to automatically select "save" and not show a dialog box and save in the folder.

I think the issue lies here in the code


.InitialView = msoFileDialogViewDetails If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub</pre>
 
Upvote 0

Forum statistics

Threads
1,217,414
Messages
6,136,497
Members
450,016
Latest member
murarj

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