VBA Userform Save Copy of Workbook

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I'm using a userform that has an option to Save a copy of the current workbook, but to give the user the option to hit save in the window themselves.

1681855017327.png


I don't have much, but in my UserForm I was going with this approach:

VBA Code:
Private Sub Export_Click()
Dim Path As String
Path = "G:\MyFolder\Prior_Months"

    ChDir Path
    ActiveWorkbook.SaveAs Filename:="G:\MyFolder\Prior_Months", FileFormat:=xlOpenXMLWorkbook

End Sub

I also have 2 ComboBoxes that lists the Month and the Year, so I'd like to have it prefill the Filename with:
"Name of File " & ComboBox_Month & " " & ComboBox_Year
Resulting in: "Name of File April 2023.xlsm"

But, I'm struggling to figure out the correctly way go about it.

Any tips, tricks or advice would be greatly appreciated!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello @Ottsel.

Try this:

VBA Code:
Private Sub Export_Click()
  Dim Path As String
  Path = "G:\MyFolder\Prior_Months"

  ChDir Path
  ActiveWorkbook.SaveAs Filename:="Name of File " & ComboBox_Month & " " & ComboBox_Year, FileFormat:=xlOpenXMLWorkbook
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
-------------
 
Upvote 0
Hello @Ottsel.

Try this:

VBA Code:
Private Sub Export_Click()
  Dim Path As String
  Path = "G:\MyFolder\Prior_Months"

  ChDir Path
  ActiveWorkbook.SaveAs Filename:="Name of File " & ComboBox_Month & " " & ComboBox_Year, FileFormat:=xlOpenXMLWorkbook
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
-------------
Hi Dante,

The name setup worked perfectly, but it didn't give me the window, but rather just saved instantly. Any ideas?
 
Upvote 0
but it didn't give me the window
Do you want the window to select the folder and file name?

Try this:
VBA Code:
Private Sub Export_Click()
  Dim sPath As String, sName As String
  Dim FileSaveName As Variant
  
  sPath = "G:\MyFolder\Prior_Months"
  sName = "Name of File " & ComboBox_Month & " " & ComboBox_Year

  FileSaveName = Application.GetSaveAsFilename(InitialFileName:=sPath & "\" & sName, _
    filefilter:="Excel Files(*.xlsx),*.xlsx", Title:="Please save the file")
  If FileSaveName = False Then Exit Sub

  ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=xlOpenXMLWorkbook
End Sub

I hope it is what you need. 😇
 
Upvote 1
Solution
Do you want the window to select the folder and file name?

Try this:
VBA Code:
Private Sub Export_Click()
  Dim sPath As String, sName As String
  Dim FileSaveName As Variant
 
  sPath = "G:\MyFolder\Prior_Months"
  sName = "Name of File " & ComboBox_Month & " " & ComboBox_Year

  FileSaveName = Application.GetSaveAsFilename(InitialFileName:=sPath & "\" & sName, _
    filefilter:="Excel Files(*.xlsx),*.xlsx", Title:="Please save the file")
  If FileSaveName = False Then Exit Sub

  ActiveWorkbook.SaveAs Filename:=FileSaveName, FileFormat:=xlOpenXMLWorkbook
End Sub

I hope it is what you need. 😇
That worked perfectly! Thank you Dante!
 
Upvote 1

Forum statistics

Threads
1,215,390
Messages
6,124,669
Members
449,178
Latest member
Emilou

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