I have two workbooks: A and B. In workbook A I want to use a command button to open workbook B. B, however, resides in Dropbox and I can't figure out how to structure the file path in the command button's VBA code so that it works on any computer.
I can't hard code the path as it appears on my work computer (e.g., C:\users\<username>\Dropbox\file.xlsm) because <username> will change when I am using my home computer. I thought that maybe I could include a Windows environment variable (e.g., %USERNAME%) in the path, but that doesn't appear to work.
Below is the command button's code that I have so far.
Private Sub CommandButton1_Click()
Dim UserPath
UserPath = "%USERNAME%"
Workbooks.Open Filename:=UserPath & "\Dropbox\Expenses.xlsm"
End Sub
When I click the button I receive a message that says, "Run-time error 1004: '%USERNAME%\Dropbox\Expenses.xlsm' could not be found." Yet this same path works splendidly when I paste it directly into Windows Explorer.
Thank you, and any suggestions are greatly appreciated!
Jeff
Bookmarks