VBA & Macro Problems

How to Copy a Worksheet from a SharePoint Macro-Enabled Workbook Using VBA

Ayan MasoodAyan Masood Oct 20, 2026 872 views

Question details

The user needs a reliable way to run a VBA macro that copies, replaces, and updates a worksheet within a macro-enabled workbook hosted on SharePoint.

How to Copy a Worksheet from a SharePoint Macro-Enabled Workbook Using VBA
Product
Microsoft Excel / SharePoint
Device & OS
not provided
Scenario
Attempting to use a VBA script to copy a worksheet from a SharePoint-hosted macro-enabled workbook (.xlsm) to another workbook, rename it, and adjust page setup settings.
Observed behavior
The VBA script executes perfectly when files are stored locally, but encounters errors, limitations, or fails to perform workbook and worksheet operations when the file is opened directly via a SharePoint URL.
Before you start

Ensure you have the OneDrive desktop application installed and signed in with the Microsoft account authorized to access your organization's SharePoint library.

Solution 1Recommended

Synchronize the SharePoint Document Library to OneDrive

By syncing the SharePoint folder to your local PC via OneDrive, you provide VBA with a local file path. This bypasses the limitations of running VBA directly against HTTPS SharePoint URLs.

VBA was originally designed for local file systems and network drives. When standard VBA commands like 'Workbooks.Open' or 'Sheets.Copy' interact directly with web-based SharePoint URLs, they frequently time out or throw errors. Syncing the files locally ensures complete compatibility with legacy VBA operations.

1
Open SharePoint in your browser

Navigate to the exact SharePoint document library where your macro-enabled workbook (.xlsm) is stored.

2
Sync the library to your PC

Click the 'Sync' button located in the top toolbar of the SharePoint document library. Confirm any prompts to open Microsoft OneDrive.

3
Locate the synced files

Open File Explorer on your computer. Look for a folder in the left navigation pane named after your organization (e.g., building icon). Inside, you will find the synced SharePoint folder.

4
Run the VBA macro locally

Open the macro-enabled workbook from this new local directory. Because the file now has a local path (like C:\Users\Name\Org\Folder\File.xlsm), your VBA code for copying worksheets and saving changes will execute seamlessly.

Synchronize the SharePoint Document Library to OneDrive
Path Independence: To make your VBA code dynamic for multiple users, utilize 'Environ("USERPROFILE")' in your script to dynamically build the path to the synced OneDrive directory.
Free Microsoft Office alternative

Experience Seamless Macro Execution with WPS Office

If SharePoint and Excel VBA restrictions are slowing down your productivity, consider WPS Office. It is a highly compatible, lightweight Office alternative that offers excellent support for local macro execution and VBA programming without the heavy overhead.

  1. 1. Download and Install: Visit the official WPS Office website to download and install the free software suite.
  2. 2. Open your Workbook: Launch WPS Spreadsheet and open your existing .xlsm macro-enabled workbook directly.
  3. 3. Run Macros Seamlessly: Navigate to the Developer tab to access the Macro editor, run your scripts, and automate your worksheet copying processes locally.
Fully compatible with Microsoft Excel formats including .xls, .xlsx, and .xlsmBuilt-in robust VBA and macro support for complex spreadsheet automationLightweight installation and incredibly fast performance for heavy data processingFamiliar user interface ensuring a seamless migration with zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA script fail when using a SharePoint URL?

Traditional VBA methods use standard file paths. SharePoint uses HTTP/HTTPS protocols, which VBA functions like 'Dir()', 'SaveAs', and 'Sheets.Copy' struggle to parse or gain write access to reliably without specialized web request coding.

Can I use Power Automate instead of VBA for SharePoint files?

Yes. Microsoft Power Automate (formerly Microsoft Flow) is natively integrated with SharePoint and is generally recommended over VBA for cloud-based file manipulation, data extraction, and moving files between cloud locations.

How do I dynamically reference the synced OneDrive folder in VBA?

Instead of hardcoding the 'C:\Users\JohnDoe\' path, you can use 'Environ("USERPROFILE") & "\Company Name\SharePoint Folder\"' in your VBA script to ensure the macro works for any user who has synced the folder.

Do macros created in Excel work in WPS Office?

Yes, WPS Office Pro and certain other versions feature extensive VBA compatibility, meaning most standard macros written in Excel (.xlsm) can run natively within WPS Spreadsheet without modification.