Check file name in VBA

StewartS

Board Regular
Joined
Feb 24, 2002
Messages
217
I have a template file which has several macros in. At present I have a short bit of code at the start of the module to check the file name is not the template file. This is to prevent users overwriting the template as each of the modules as a save at the end.

WKB = ActiveWorkbook.name

If WKB = "Template 17-18.xlsm" Then

MsgBox ("Please save journal as a different file before proceeding")
Exit Sub

End If

My question is would this work as a function and exit the routine if required.

Thanks

StewartS
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I've manged to solve this

I added a function to test the file name which is called in the indivdual macros and added just the message and and exit sub routine into the macros

Function testfile()
Wkb = ActiveWorkbook.Name
If Wkb = "Template.xlsm" Then
testfile = True
End If
End Function

Sub test2()

If testfile = True Then
MsgBox ("Please save journal as a different file before proceeding")
Exit Sub
End If

Code.....

End sub

This means the workbook name is changed in one place when it is renamed each year.

StewartS
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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