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

d0rian

Active Member
Joined
May 30, 2015
Messages
261
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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

d0rian

Active Member
Joined
May 30, 2015
Messages
261
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
370
Office Version
  1. 2016
Platform
  1. Windows
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

Active Member
Joined
May 30, 2015
Messages
261
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
370
Office Version
  1. 2016
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,454
Messages
5,596,228
Members
414,047
Latest member
debbos

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