VBA Save As with User prompted to select folder path

lwebbie

New Member
Joined
Feb 28, 2017
Messages
24
I have a macro that uses the GetSaveAsFilename which is fine except it makes the user type in the file name to save the file. The issue is people mistype the file name which messes up other macros down the line. The file name never changes but the folder location does so the user needs to be able to select the folder to save the file in but the file name will always be "Device Complianc.xlsx". One other thing to note the file they are doing a Save As to has the original format as .xls.

Here is the current code:
Sub sbSaveExcelDialog()
Dim IntialName As String
Dim sFileSaveName As Variant
IntialName = "Device Compliance"
sFileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, fileFilter:="Excel Files (*.xlsx), *.xlsx")
If sFileSaveName <> False Then
ActiveWorkbook.SaveAs sFileSaveName
End If
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How about
Code:
Sub sbSaveExcelDialog()
   Dim Fldr As String

   With Application.FileDialog(4)
      .AllowMultiSelect = False
      If .Show <> -1 Then Exit Sub
      Fldr = .SelectedItems(1)
   End With

   ActiveWorkbook.SaveAs Fldr & "\Device Compliance.xlsx", 51

End Sub
 
Upvote 0
For whatever reason, it does not like using a variable for the Initial Name.
Code:
Sub sbSaveExcelDialog()
 Dim IntialName As String
 Dim sFileSaveName As Variant
 'IntialName = "Device Compliance.xlsx"
 sFileSaveName = Application.GetSaveAsFilename("Device Compliance.xlsx", "Excel Files (*.xlsx), *.xlsx")
     If sFileSaveName <> False Then
         ActiveWorkbook.SaveAs sFileSaveName
     End If
 End Sub
This displayed the file name as the Initial Name.
 
Upvote 0
VBA Code:
Sub sbSaveExcelDialog()
   Dim Fldr As String

   With Application.FileDialog(4)
      .AllowMultiSelect = False
      If .Show <> -1 Then Exit Sub
      Fldr = .SelectedItems(1)
   End With

   ActiveWorkbook.SaveAs Fldr & "\Device Compliance.xlsx", 51

End Sub

How do you get this so it doesn't close the file that you save the file from?


Steve
 
Upvote 0
You don't, that's how SaveAs works. You could use SaveCopyAs, but I don't think you can change the filetype.
 
Upvote 0
You can add a line after the SaveAs to re-open the original workbook.
 
Upvote 0

Forum statistics

Threads
1,214,894
Messages
6,122,124
Members
449,066
Latest member
Andyg666

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