Excel vba save active workbook with new name
You can use the following VBA code to save the active workbook with a new name:
Sub SaveAsNewName()
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.SaveAs "C:\NewFileName.xlsx" 'change the path and file name as needed
End Sub
This code uses the SaveAs
method to save the active workbook with a new name. You'll need to replace "C:\NewFileName.xlsx"
with the desired path and file name.
If you want to prompt the user to enter the new file name, you can use the InputBox
function:
Sub SaveAsNewName()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim newFileName As String
newFileName = InputBox("Enter the new file name:", "Save As")
wb.SaveAs newFileName & ".xlsx" 'add the file extension
End Sub
This code will prompt the user to enter a new file name, and then save the workbook with that name.
You can also use the FileDialog
object to save the workbook with a new name:
Sub SaveAsNewName()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "NewFileName"
fd.InitialDir = "C:\"
If fd.Show Then
wb.SaveAs fd.SelectedItems(1)
End If
End Sub
This code uses the FileDialog
object to display a "Save As" dialog box, where the user can enter a new file name and select a location to save the file. The SaveAs
method is then used to save the workbook with the new name.