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

d0rian

Board Regular
Joined
May 30, 2015
Messages
241
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...
 

d0rian

Board Regular
Joined
May 30, 2015
Messages
241
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
290
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
241
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
290
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,084,744
Messages
5,379,581
Members
401,614
Latest member
priokatm

Some videos you may like

This Week's Hot Topics

  • VBA code giving errors and stopping Excel
    Hello Experts, I have this code being used to loop through files in a file path, and copy specific data to another sheet. It is giving me several...
  • Disable MsgBox message
    Morning, I have a userform where if i leave a ComboBox empty i see a MsgBox warning me that i must enter an invoice number. It is this MsgBox i...
  • Macro Recorder into VBA, Copy Paste Data Filled Cells
    Hi Everyone, I have a macro recorder file that takes a selection of data, copies, then pastes into a new sheet on ("A2:B2") The issue is my...
  • Number format changes while pasting into a cell
    Hi, I am trying to paste a number 180204524303 from an email to an excel cell, however, whenever i try to do so , the the paste value appears as...
  • Collating data
    Hello all. Could someone please help. I am trying to pull all column data from multiple sheets (24 I total so far) into 1 master sheet without...
  • Sum Multiple Columns Based on Multiple Criteria
    I am trying to consolidate data by summing columns G through M based on material, plant, vendor, and fiscal year being identical. The period does...
Top