Use the Current Windows Username in an Excel VBA File Path
Question details
Build an Excel VBA path that uses the current Windows user's profile instead of a hard-coded username.

- Product
- Microsoft Excel VBA
- Device & OS
- Windows
- Scenario
- A macro needs to access a locally synced OneDrive folder for multiple users.
- Observed behavior
- The path should resolve to each user's profile while keeping the shared OneDrive folder name and subfolder.
In File Explorer, confirm the exact name of the locally synced OneDrive folder and its PDF's subfolder. Each user must have the same folder structure under their Windows profile for the example path to work.
Build the OneDrive path from the Windows user profile
Use the USERPROFILE environment variable to replace the fixed C:\Users\Bill portion of the path.
In Excel, press Alt+F11. In the VBA editor, select Insert > Module if you need a new module.
In your macro, replace the hard-coded path with: folderPath = Environ("USERPROFILE") & "\OneDrive - Folder two\PDF's". Declare folderPath as a String if it is not already declared.
Use folderPath in the macro where the fixed path was used. To check the result, add MsgBox folderPath and run the macro with F5 in the VBA editor.
Compare the displayed path with the folder in File Explorer. If necessary, change "OneDrive - Folder two\PDF's" to match the actual local folder structure.

Work with spreadsheets in WPS Office
WPS Office is a free, lightweight alternative for working with spreadsheets in a familiar interface. Its support for common Excel formats makes it easier to move existing files, though VBA availability and compatibility can vary.

Frequently Asked Questions
What does Environ("USERPROFILE") return?
It returns the current Windows user's profile path, such as C:\Users\Bill. Use it in the VBA editor to build a path without hard-coding that username.
Will this path work if users have different OneDrive folder names?
Not without changes. Check each local folder name in File Explorer; the text appended after Environ("USERPROFILE") must match the user's actual folder structure.
How can I check the path my macro creates?
Add MsgBox folderPath after assigning the variable, then run the macro with F5 in the VBA editor. Compare the displayed path with the folder in File Explorer.
What file type should I use to save the macro?
In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) to retain the VBA code.




