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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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