How to define Dropbox folder path in VBA for access by different users/usernames

d0rian

Board Regular
Joined
May 30, 2015
Messages
243
When you install Dropbox, it generates a folder path that incorporates your Windows username. So on my main computer, the path is:

C:\Users\peter\Dropbox\files\

but on my secondary computer (different Windows username but access to the SAME dropbox folder), it's:

C:\Users\david\Dropbox\files\

I use this VBA code at the top of one of my macros:

Code:
    Dim MyPath As String
    Dim MyFileName As String
    MyPath = "C:\Users\peter\Dropbox\files\"
    MyFileName = "data_file.csv
But the problem is that I can't run this macro from my secondary ('david') computer, because the Dropbox file path is fixed/absolute and thus is not recognized from that computer...how can I edit the code so that it uses the correct Dropbox file path regardless of what computer I'm accessing it from?

I thought I'd found a relevant thread, but my attempt to port the advice, and change the 3rd line of code above to:
Code:
MyPath = "C:\Users\" & Environ("USERPROFILE") & "\Dropbox\files\"
did not work...
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

d0rian

Board Regular
Joined
May 30, 2015
Messages
243
Oh, and I have the same question when it comes to Data Connections...when I use the Excel menus to create a new connection (Connections > Add... > Browse for more...) and navigate to the file I want to create a connection to (a file in my Dropbox folder), it populates the Connection source file using the fixed/absolute path (e.g. C:\Users\peter\Dropbox\files\data_file.csv)

So again, this makes me unable to refresh this data connection when I'm on a different computer...and unlike the OP above, I'm not using VBA, so it's not immediately clear how I can create a Connection to a Dropbox file while making that connection accessible on a computer with a different user name (and Dropbox file path)...
 

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
302
But the problem is that I can't run this macro from my secondary ('david') computer, because the Dropbox file path is fixed/absolute and thus is not recognized from that computer...how can I edit the code so that it uses the correct Dropbox file path regardless of what computer I'm accessing it from?

I thought I'd found a relevant thread, but my attempt to port the advice, and change the 3rd line of code above to:
Code:
MyPath = "C:\Users\" & Environ("USERPROFILE") & "\Dropbox\files\"
did not work...

Change to Environ("USERPROFILE") & "\Dropbox\files" or change userprofile to username
 
Last edited:

d0rian

Board Regular
Joined
May 30, 2015
Messages
243
Thank you, the first of your suggestions worked. So if I'm understanding correctly, for any file path in VBA, if I don't want to use an absolute path name just in case it's code I want to run on a different computer (with a different username)

Environ("USERPROFILE") &

will substitute for

"C:\Users\[whatever_the_logged_in_username_is]" ?

(As to your second suggestion, just so I fully understand what you were suggesting, were you saying that I could simply type in USERNAME instead of USERPROFILE in the code that I had said in the OP did not work? Because I tried that, but it generated an error again...or were you suggesting changing my computer's WIndows username to match what it was (peter, not david) on my main computer?)
 

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
302
Thank you, the first of your suggestions worked. So if I'm understanding correctly, for any file path in VBA, if I don't want to use an absolute path name just in case it's code I want to run on a different computer (with a different username)

Environ("USERPROFILE") &

will substitute for

"C:\Users\[whatever_the_logged_in_username_is]" ?

(As to your second suggestion, just so I fully understand what you were suggesting, were you saying that I could simply type in USERNAME instead of USERPROFILE in the code that I had said in the OP did not work? Because I tried that, but it generated an error again...or were you suggesting changing my computer's WIndows username to match what it was (peter, not david) on my main computer?)
It's strange that the second suggestion didn't work.
As a test, in the immediate window use
?Environ("USERNAME") which will provide the username of the current user
?Environ("USERPROFILE") path to the user profile ie C:\Users\Username
 

Forum statistics

Threads
1,089,283
Messages
5,407,366
Members
403,137
Latest member
ExcelGzh

This Week's Hot Topics

Top