Display filename in input box and save

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
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.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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
 
Upvote 0
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:
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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