To save a file with a specific name by input box in VBA.
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”
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
- User Input is helpful when you are saving a file, or you want to input a value like date, year, or category etc.
- Always handle the condition when the user does not enter any input.
Post you may like