Persistent public variable

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Maybe I'm truly not understanding other answers I've come across but I'm hoping someone can dumb this down for me.

My work uses roaming profiles and therefore Environ$("HOMESHARE") gives me the proper path for My Documents or Downloads. However, when I'm using my work laptop (or copy Personal.xlsb to my personal laptop), Environ$("USERPROFILE") gives the appropriate path.

What I'd like to do is create a persistent public variable that will allow me to assign a string based on which Application.StartupPath is present. In other words:
VBA Code:
If Application.StartupPath = workpath then
   sEnviron = Chr(34) & "HOMESHARE" & Chr(34)
ElseIf Application.StartupPath = laptoppath then
   sEnviron = Chr(34) & "USERPROFILE" & Chr(34)
End If

From there, through out my coding, I use Environ$ in various Subs/Functions. I'd like to use sEnviron even though the Sub where Environ has ended. sEnviron would persist as long as Excel was open, regardless of how many subs or functions were run. I'm not talking about passing the variable in a series of subs, but rather once the defining sub ends, having the Public variable always assigned.

Is this even possible? I'm open to pushing the variable to the Personal.xlsb worksheet but I don't know how to define it once and then it's always available.

Thanks y'all.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about defining this function in a satandrd module in your workbook project :
VBA Code:
Public Function sEnviron() As String
    If Application.StartupPath = workpath Then
       sEnviron = Chr(34) & "HOMESHARE" & Chr(34)
    ElseIf Application.StartupPath = laptoppath Then
       sEnviron = Chr(34) & "USERPROFILE" & Chr(34)
    End If
End Function

Then you can simply call the above sEnviron function throughout your Subs/Functions .
 
Upvote 0
Oh wow! Thank you, Jaafar. I got so stuck seeing only a single solution.

I love this board!!
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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