Display filename in input box and save

Shadkng

Board Regular
Joined
Oct 11, 2018
Messages
235
Hi, after searching online I see there are ways to have a macro bring up an input box where a filename can be entered and saved. Is there a way to have the input box display the current filename so I can just edit the name and save as? This would avoid having to type long filenames. The path would remain the same Additionally, if the filename is not edited can the save be nullified in order not to overwrite it the original file? Thanks very much.
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
Add a button and put this code behind it
VBA Code:
Private Sub ButtonName_Click()
    Dim fName As String
    With ThisWorkbook
        fName = Left(.Name, (InStrRev(.Name, ".") - 1))
        On Error Resume Next
        .SaveAs .Path & "\" & InputBox("Enter new name ...", "Save As", fName)
        On Error GoTo 0
    End With
End Sub

NOTE : above gives the user the option not to overwrite the file if existing name used
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
To prevent user saving the file with the CURRENT file name ...

VBA Code:
Private Sub ButtonName_Click()
    Dim fName As String, newName As String
    With ThisWorkbook
        fName = Left(.Name, (InStrRev(.Name, ".") - 1))
        newName = InputBox("Enter new name ...", "Save As", fName)
        If Not newName = fName Then
            On Error Resume Next
            .SaveAs .Path & "\" & newName
            On Error GoTo 0
        Else
            MsgBox "cannot overwrite current file name", vbExclamation, "oops"
        End If
    End With
End Sub

NOTE :
- Above allows user to overwrite other files if name already exists in the folder - the standard warning appears
- Save and SaveAs options work as normal

Q Do you want to prevent the standard Excel Save button from allowing the file to be saved under current name?
 
Last edited:

Shadkng

Board Regular
Joined
Oct 11, 2018
Messages
235
To prevent user saving the file with the CURRENT file name ...

VBA Code:
Private Sub ButtonName_Click()
    Dim fName As String, newName As String
    With ThisWorkbook
        fName = Left(.Name, (InStrRev(.Name, ".") - 1))
        newName = InputBox("Enter new name ...", "Save As", fName)
        If Not newName = fName Then
            On Error Resume Next
            .SaveAs .Path & "\" & newName
            On Error GoTo 0
        Else
            MsgBox "cannot overwrite current file name", vbExclamation, "oops"
        End If
    End With
End Sub

NOTE :
- Above allows user to overwrite other files if name already exists in the folder - the standard warning appears
- Save and SaveAs options work as normal

Q Do you want to prevent the standard Excel Save button from allowing the file to be saved under current name?
 

Shadkng

Board Regular
Joined
Oct 11, 2018
Messages
235
Hi Yongle, both programs ran fine and as you explained. I have to incorporate one of them into my macros. I'm sure it will be fine...I will come back to you if I need any additional help
Thanks a lot for your help.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,705
Messages
5,597,666
Members
414,162
Latest member
jborjal1967

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
Top