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

d0rian

Active Member
Joined
May 30, 2015
Messages
313
Office Version
  1. 365
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...
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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)...
 
Upvote 0
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:
Upvote 0
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?)
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,152
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