Save a file with a specific name by input box in VBA

Save User File by input box vba

Objective

To save a file with a specific name by input box  in VBA.

Approach

Here we shall open a file, then a user input box will be shown.Post which we shall enter a new name, and that input is used while saving the file.In the image below we can see that we have provided user input as “Sample User Input”. So new saved workbook will have name as “Sample User Input.xlsx”

 

Save User File by input box vba

Code

Sub InputBox()
Dim MyTargetFile As Variant
Dim UserInput As Variant
'You can change *.xlsx to *.csv or any other format
MyTargetFile = Application.GetOpenFilename("Excel Files (*.xlsx), *xlsx")
'First open a excel file
Workbooks.Open Filename:=MyTargetFile
'Give a name to the opened excel file
MyOpenedFile = ActiveWorkbook.Name
'Here we are trying to get a file name from user, which we will use when we save a file
UserInput = Application.InputBox(prompt:="Provide a file name to save")
If UserInput <> "" Then
'Save the opened excel file by renaming it , here UserInput is used  as new file name.
Workbooks(MyOpenedFile).SaveAs Filename:=ThisWorkbook.Path & "\" & UserInput
End If
'Close the saved workbook
ActiveWorkbook.Close
End Sub

 

Notes

  1. User Input is helpful when you are saving a file, or you want to input a value like date, year, or category etc.
  2. Always handle the condition when the user does not enter any input.

Post you may like

Import, crop, rotate, delete, position the image in VBA Excel

1 comment on “Save a file with a specific name by input box in VBA

Comments are closed.