File Path Outside of Subroutine

Braunschweiger

Board Regular
Joined
Feb 19, 2014
Messages
104
Hello,
I need help figuring out how to place a variable file path in it's own location so that, other subroutines can go to that file path in order to open different excel workbooks stored in the folder the variable file path points to. This will help me be more efficient. If the file path changes (e.g., I place the folder that contains my excel workbooks in a different location), I won't have to go through my entire code and change the file path in every subroutine.

The code below works. It follows the file path "C:\Users\david\Documents\a.Portfolio Snapshot Template\Dumps\" and opens workbook "dbo_active_financing.xlsx"
My problem is...the entire code is contained in 1 sub.
VBA Code:
Public Sub openfile_THIS_WORKS_v2_USE_THIS_ONE()

Dim strFName As String 'One Sub
Dim myVar As String 'Many Subs
Dim wkb As Workbook 'One Sub
Dim sht As Worksheet 'One Sub

myVar = "C:\Users\david\Documents\a.Portfolio Snapshot Template\Dumps\" 'Many Subs
strFName = myVar & "dbo_active_financing.xlsx" 'One Sub

Set wkb = Workbooks.Open(strFName) 'One Sub
Set sht = wkb.Sheets("dbo_active_financing") 'One Sub
sht.Activate 'One Sub

End Sub

What I would like to do is, place the below code in it's own location so that other subroutines can use it.
Code:
Dim myVar As String 'Many Subs
myVar = "C:\Users\david\Documents\a.Portfolio Snapshot Template\Dumps\" 'Many Subs

So, the code below (which is actually the first code presented without the second code) could acquire the file path (along with any other subroutine) "C:\Users\david\Documents\a.Portfolio Snapshot Template\Dumps\" which is contained in it's own location.
VBA Code:
Public Sub openfile_THIS_WORKS_v2_USE_THIS_ONE()

Dim strFName As String 'One Sub
Dim wkb As Workbook 'One Sub
Dim sht As Worksheet 'One Sub

strFName = myVar & "dbo_active_financing.xlsx" 'One Sub

Set wkb = Workbooks.Open(strFName) 'One Sub
Set sht = wkb.Sheets("dbo_active_financing") 'One Sub
sht.Activate 'One Sub

End Sub

Thanks in advance!
Dave
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
Office Version
  1. 365
Platform
  1. Windows
There is no difference between a Public & Global variable, at least none that I know of.
As long as it's in a standard module, it will be available to all the code in the workbook.
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,414
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Global and Public have the same effect. Global is quite old-fashioned. ;)
 

Braunschweiger

Board Regular
Joined
Feb 19, 2014
Messages
104
Thanks everyone for the education. I can't tell you how valuable this forum is to me. Thanks again!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,928
Messages
5,621,642
Members
415,849
Latest member
PhoenixRising2015

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
Top