Need vba code to open save as box and prefill file path only

mayoung

Active Member
Joined
Mar 26, 2014
Messages
259
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Is it possible to use VBA to open the save as dialog box with the file path already chosen so all the user has to do is name the file and pick file extension and save?

also if this can can done can the file extension be pre-chosen also?

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This code worked perfectly!!! Thank You so much Yongle....
 
Upvote 0
You are welcome. Thanks for the feedback (y)
 
Upvote 0
Yongle,
Sent You a Private Message please check..
Thanks
 
Upvote 0
One way:
- go to post#7 above
- left-click on your name
- select View Forum Posts

So Yongle this code you sent works great one variation i am looking for. Anyway to have the file name to pre-populate when I save as?

Sub sbSaveExcelDialog()

Dim wb As Boolean
ChDir MY PATH"
wb = Application.Dialogs(xlDialogSaveAs).Show(Arg2:=xlCSV)
End Sub
 
Upvote 0
Are you looking for something like this?
VBA Code:
Sub sbSaveExcelDialog()
    Dim bResult     As Boolean
    Dim sFileName   As String
    Dim sPath       As String

    sPath = "YOUR PATH"
    sFileName = "MyFileName.csv"
    
    ChDir sPath
    bResult = Application.Dialogs(xlDialogSaveAs).Show(Arg1:=sFileName, Arg2:=xlCSV)
End Sub
 
Upvote 0
Did you change the sPath= and sFileName= lines according to your situation? What exactly doesn't work? Are you getting an error?
 
Upvote 0
I think its my file name. trying to have it say a fixed text and then the date so example:

TEST_032220

sFileName = "Test_" & "Format (NOW() "MMDDYY")"
 
Upvote 0
That should not be the cause of the problem, unless you did not provide a "\" between your path and your filename. Anyway, sometimes this dialog function can be doing unexpected things. The following might be a better alternative. This procedure saves the active workbook based upon the user's choice with a pre populated Save As Dialog.
VBA Code:
Public Sub Example()

    Dim oWb         As Workbook
    Dim sMyFile     As String
    Dim sSavedFile  As String

    sMyFile = "C:\Users\ItalianPlatinum\Documents\MyData.CSV"   ' <<< change as required
   
    Set oWb = ActiveWorkbook                                    ' <<< change as required

    ' return with drive:\folder\filename.ext  of saved file
    sSavedFile = FileSaveAs(oWb, sMyFile)

    Set oWb = Nothing
End Sub


Public Function FileSaveAs(argWbk As Workbook, argFileFullName As String) As String

    Dim sFileName   As String
    Dim sFileExt    As String
    Dim sFilters    As String
    Dim iFiltIx     As Integer
    Dim vFormat     As Long

    ' compose string of used formats
    sFilters = "Excel Workbook (*.xlsx),*.xlsx," + _
               "Excel Macro-Enabled Workbook (*.xlsm),*.xlsm," + _
               "CSV (Comma delimited)(*.csv),*.csv,"
    ' get initial file extension
    sFileExt = Right(argFileFullName, Len(argFileFullName) - InStrRev(argFileFullName, "."))
    ' set current filter accordingly
    Select Case LCase(sFileExt)
        Case "xlsx":    iFiltIx = 1
        Case "xlsm":    iFiltIx = 2
        Case "csv":     iFiltIx = 3
        Case Else:      iFiltIx = 1
    End Select
    ' user provides final folder and file name
    sFileName = Application.GetSaveAsFilename(InitialFileName:=argFileFullName, _
                                              FileFilter:=sFilters, _
                                              FilterIndex:=iFiltIx)
    If sFileName = "False" Then
        MsgBox "User has canceled.", vbExclamation, "SaveAs"
        FileSaveAs = ""
        GoTo SUB_DONE
    End If
    ' get final file extension
    sFileExt = Right(sFileName, Len(sFileName) - InStrRev(sFileName, "."))
    ' save file as requested
    Select Case LCase(sFileExt)
        Case "xlsx":    vFormat = xlOpenXMLWorkbook
        Case "xlsm":    vFormat = xlOpenXMLWorkbookMacroEnabled
        Case "csv":     vFormat = xlCSV
        Case Else:      vFormat = xlOpenXMLWorkbook
    End Select
    argWbk.SaveAs Filename:=sFileName, FileFormat:=vFormat
    FileSaveAs = sFileName
SUB_DONE:
End Function
 
Upvote 0

Forum statistics

Threads
1,216,118
Messages
6,128,939
Members
449,480
Latest member
yesitisasport

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