How to use VBA to close ThisWorkbook if the folderpath it's opened from is not the specified folderpath?

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

Have checked Google and come a blank on this one.
Does anyone know if it's possible to use VBA---in a Private Sub Workbook_Open() macro---to check if the filepath that the file has been opened from matches a specified path, and if not, close the workbook?

Kind regards,

Doug.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
How about
VBA Code:
Private Sub Workbook_Open()
   If Me.Path <> "Your file path" Then Me.Close False
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub Workbook_Open()
   If Me.Path <> "Your file path" Then Me.Close False
End Sub

Hi Fluff,

Thanks for that, works perfectly.
Was wondering about the Me.Path syntax: when used in an ordinary module
VBA Code:
MsgBox Me.Path
, it says the syntax is incorrect, but it works in the ThisWorkbook window, so what does "Me." actually mean in VBA?

Kind regards,

Doug.
 
Upvote 0
Me refers to different things,
In the ThisWorkbook module it refers to the Workbook.
In a sheet module it's the sheet
In a UserForm module it's the UserForm.
 
Upvote 0
Me refers to different things,
In the ThisWorkbook module it refers to the Workbook.
In a sheet module it's the sheet
In a UserForm module it's the UserForm.

Hi Fluff,

Thanks for explaining it to me: I very much appreciate your teaching work!

Kind regards,

Doug.
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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