VBA for Save as to open open dialog box, populate filename and select same folder as open workbook

leojez

Board Regular
Joined
Apr 12, 2022
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Hello, been trying to figure this out but can't quite get there.

I have a workbook and I've added a button with the following macro attached:

VBA Code:
Sub Save_settings_button()
'
' Save_settings_button
'

'

Worksheets(1).Activate
Application.GetSaveAsFilename

End Sub

This works fine, in that it opens the Save as dialog, however, the filename field is blank, and I'd like this to populate the field with the filename "Portfolio Balance Tool.xlsm" - so the user can then change the filename as required.

Also, the file location goes to my documents folder by default, and I'd like it to select the folder of the open workbook.

Any ideas to update my VBA code? Thank you!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

leojez

Board Regular
Joined
Apr 12, 2022
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Hoping for some ideas. Anything? Thanks!
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,991
Office Version
  1. 2016
Platform
  1. Windows
Something like this:
VBA Code:
Sub test()
    Dim vFileSaveName As Variant
    Dim sCurDir As String
    
    On Error GoTo Xit
    
    sCurDir = CurDir
    ChDir ThisWorkbook.Path
    vFileSaveName = Application.GetSaveAsFilename("Portfolio Balance Tool.xlsm", "Excel Files (*.xlsm), *.xlsm")
    
    If vFileSaveName <> False Then
        'save file here.
    Else
        'you cancelled.
    End If
    
Xit:
    ChDir sCurDir
End Sub
 

leojez

Board Regular
Joined
Apr 12, 2022
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Thanks so much for replying! Actually after much trial and error I came up with this, this morning:

VBA Code:
Application.Dialogs(xlDialogSaveAs).Show ("c:\my_folder\Portfolio Balance Tracker")

It seems super simple compared to yours. It does exactly what I want ... do you think there could be anything than could cause issues as it is so simple? Thanks.
 

Forum statistics

Threads
1,181,639
Messages
5,931,158
Members
436,779
Latest member
linlaz

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
Top