excel workbook: do not allow certain file name

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hello all,


Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved is abc123.xlsx

A msgbox to notify user to change the filename, if this is the case.

Is it possible?
Thank you and will appreciate.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Glad you got this working in the end and thanks for the feedback.

Oh I thoroughly checked the code and it will give me a MsgBox in every instance no matter what I named the file with. What am I be doing wrong here?

VBA Code:
Repeat:
    userResponce = Application.GetSaveAsFilename(InitialFileName:="%USERPROFILE%\Desktop\Monthly Sales Report - month year", _
    fileFilter:="Excel Workbook(*.xlsx), *.xlsx")
    If userResponce = "False" Then
        Exit Sub
    Else
        If userResponce <> "Monthly Sales Report - month year" Then
        MsgBox "Please add month and year to the filename"
        GoTo Repeat
        End If
 
Upvote 0
Monthly Sales Report - May 2020
not permitting the above.
in that case this should work :
VBA Code:
Repeat:
 
    userResponce = Application.GetSaveAsFilename(InitialFileName:="\Desktop\Monthly Sales Report - Month Year", _
    fileFilter:="Excel Workbook(*.xlsx), *.xlsx")
    If userResponce = "False" Then
        Exit Sub
    Else
    
    If userResponce = "Monthly Sales Report - May 2020" Then
        MsgBox "Wrong file name"
        GoTo Repeat
    End If
 
Upvote 0
in that case this should work :
VBA Code:
Repeat:

    userResponce = Application.GetSaveAsFilename(InitialFileName:="\Desktop\Monthly Sales Report - Month Year", _
    fileFilter:="Excel Workbook(*.xlsx), *.xlsx")
    If userResponce = "False" Then
        Exit Sub
    Else
   
    If userResponce = "Monthly Sales Report - May 2020" Then
        MsgBox "Wrong file name"
        GoTo Repeat
    End If
Yes but all filenames are permitted now. the logical test is either accepting all or rejecting all with = and <> respectively
 
Upvote 0
Yes but all filenames are permitted now. the logical test is either accepting all or rejecting all with = and <> respectively

Maybe I didn't understand... In post#14 you said that "Monthly Sales Report - May 2020 " is the file name that is not permitted .. So if
userResponce = "Monthly Sales Report - May 2020" then display msgBox and display the save dialog again.
 
Upvote 0
Sorry I overlooked the file extension

Try this variation :
VBA Code:
Repeat:
 
    userResponce = Application.GetSaveAsFilename(InitialFileName:="Monthly Sales Report - May 2020", _
    fileFilter:="Excel Workbook(*.xlsx), *.xlsx")
    If userResponce = "False" Then
        Exit Sub
    Else
    
    If InStr(1, userResponce, "Monthly Sales Report - May 2020", vbTextCompare) Then
        MsgBox "Wrong file name"
        GoTo Repeat
    End If
 
Upvote 0
Sorry I overlooked the file extension

Try this variation :
VBA Code:
Repeat:

    userResponce = Application.GetSaveAsFilename(InitialFileName:="Monthly Sales Report - May 2020", _
    fileFilter:="Excel Workbook(*.xlsx), *.xlsx")
    If userResponce = "False" Then
        Exit Sub
    Else
   
    If InStr(1, userResponce, "Monthly Sales Report - May 2020", vbTextCompare) Then
        MsgBox "Wrong file name"
        GoTo Repeat
    End If
OKi it is closer now. But there 2 problems.
1. the default location is not set to desktop
2. the file with name "Monthly Sales Report - May 2020 Sunday" will still be rejected even though it has changed.

for the second part if the user removes 2020 and then add sunday this will work
"Monthly Sales Report - May Sunday"

Could it be also taken care of please.
 
Upvote 0
For question 1, just change the initial filename to this "%USERPROFILE%\Desktop\Monthly Sales Report - month year".

As for your second question, I am getting confused and I am not sure I understand what is supposed to happen. Can you explain what the scenario is ?
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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