VBA & Macro Problems

Use the Current Windows Username in an Excel VBA File Path

Jason MillerJason Miller Nov 18, 2026 872 views

Question details

Build an Excel VBA path that uses the current Windows user's profile instead of a hard-coded username.

Use the Current Windows Username in an Excel VBA File Path
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, press Alt+F11. In the VBA editor, select Insert > Module if you need a new module.

2
Construct the path

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.

3
Use and test the path

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.

4
Verify the folder

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.

Build the OneDrive path from the Windows user profile
Folder names must match: USERPROFILE supplies the current user's profile path, but it does not identify the OneDrive folder automatically. The appended folder name must match each user's locally synced folder.
Free Microsoft Office alternative

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.

Open and edit spreadsheets using a familiar interfaceCompatible with common Microsoft Excel formats, including XLSX and XLSUse a free, lightweight office suite for everyday spreadsheet workMove existing spreadsheet files into WPS Office without starting from scratch
microsoft office alternative - wps office

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.