logo
search
VBA & Macro Problems

Fix Excel VBA Save As Macro for User Documents Folder

Elise WilliamsElise Williams Oct 13, 2026 868 views

Question details

The user needs to modify a VBA macro so it can successfully save an Excel workbook to the current user's Documents folder instead of failing due to a hardcoded path.

Fix an Excel VBA Save As Macro for Each User's Documents Folder
Product
Excel
Device & OS
not provided
Scenario
Sharing a macro-enabled workbook where a VBA script reads a file name from cell B1 and saves the file locally. The script works for the original author but fails for anyone else.
Observed behavior
The macro triggers a runtime error at the Save As line when run by other users, typically caused by incorrect user-specific paths, missing folders, or invalid file names.
Before you start

Verify that cell B1 contains a valid file name without prohibited characters (like slashes or asterisks) and ensure the destination folder structure actually exists on the target computers.

Solution 1Recommended

Use Environ Function for Dynamic User Paths

Replace the hardcoded author path with a dynamic variable that automatically detects the current user's Windows profile directory.

When you record or write a macro on your own computer, the path often includes your specific Windows username (e.g., C:\Users\JohnDoe). When another user runs it, Excel cannot find that directory. Using the VBA Environ("UserProfile") function resolves this by fetching the active user's directory dynamically.

1
Open the VBA Editor

Press ALT + F11 in Excel to open the Visual Basic for Applications editor, and locate the module containing your Save As macro.

2
Declare Path Variables

At the beginning of your subroutine, define string variables for the paths by typing: Dim userPath As String and Dim savePath As String.

3
Assign the Dynamic Profile Path

Use the environment variable to get the user's base folder by adding the line: userPath = Environ("UserProfile").

4
Construct the Full Save Path

Append the specific destination folder to the user path. For example: savePath = userPath & "\Documents\Excel\".

5
Execute the SaveAs Command

Update your SaveAs line to combine the dynamic path with the filename from your worksheet: ActiveWorkbook.SaveAs Filename:=savePath & ActiveSheet.Range("B1").Value, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False.

Use Environ Function for Dynamic User Paths
Testing the Macro: Always test the macro on a different user profile if possible, ensuring the subfolders (like \Documents\Excel\) are already created, or add a VBA MkDir command to create them automatically.
WPS Spreadsheet Macro Support

Run and Edit VBA Macros Seamlessly in WPS Office

WPS Office provides robust support for VBA and Excel macros. You can edit, run, and troubleshoot your dynamic Save As macros directly within WPS Spreadsheet without changing your original code.

  1. 1. Open the Workbook: Launch WPS Spreadsheet and open your macro-enabled workbook (.xlsm).
  2. 2. Access Developer Tools: Navigate to the 'Developer' tab on the top ribbon.
  3. 3. Open the VBA Editor: Click on 'Macros' or 'Visual Basic' to open the built-in VBA editor.
  4. 4. Run Your Code: Paste your updated dynamic path macro code using Environ("UserProfile") and click Run to execute it seamlessly.
Fully compatible with Microsoft Excel VBA syntax and macro-enabled workbooks (.xlsm).Built-in VBA editor for writing and debugging dynamic path scripts.Lightweight alternative with a highly familiar spreadsheet interface.Seamless migration of your automated reports and custom VBA tools.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Excel macro say 'Path not found' for other users?

This usually happens because the macro contains a hardcoded file path containing the original author's username (e.g., C:\Users\JohnDoe\Documents). When a user named Jane runs it, her computer cannot find 'JohnDoe's' folder. Using Environ("UserProfile") fixes this by dynamically finding the current user's folder.

How can I automatically create the destination folder if it doesn't exist?

You can use the VBA 'Dir' function to check if the folder exists (e.g., If Dir(savePath, vbDirectory) = "" Then). If it returns an empty string, use the 'MkDir' function (e.g., MkDir savePath) to create the folder before executing the Save As command.

What file format should I use when saving macro-enabled workbooks via VBA?

When saving a workbook that contains macros, you should use FileFormat:=xlOpenXMLWorkbookMacroEnabled (which also corresponds to the numeric value 52). This ensures the workbook is saved correctly as an .xlsm file and retains its macro functionality.

Does Environ("UserProfile") work on Mac computers?

No, the Environ function is specific to Windows operating systems. If you need to write a macro for Mac users, you must use MacScript, AppleScriptTask, or the Application.DefaultFilePath property to determine the appropriate local directories.