logo
search
VBA & Macro Problems

Fix Excel VBA StockHistory Corrupting XLSM and XLSB Files

Emma BrownEmma Brown Sep 30, 2026 868 views

Question details

Users experience file corruption, save errors, and data loss when running the Excel VBA Application.StockHistory function within XLSM or XLSB files stored on OneDrive.

Fix Excel VBA StockHistory Corrupting XLSM and XLSB Files
Product
Microsoft Excel
Device & OS
Windows 11
Scenario
Running a macro that calls Application.StockHistory in a workbook synced to OneDrive.
Observed behavior
The workbook encounters save errors, requires file repair, loses data, or suffers from severe file corruption.
Before you start

Immediately stop running the macro on your live production files to prevent further data loss, and create secure local backups of your workbooks.

Solution 1Recommended

Isolate the Macro and Report to Microsoft

Since this is an identified bug in Microsoft 365, the best approach is to stop using the function on critical files and report your findings to Microsoft for a permanent patch.

This issue is a known conflict between the Application.StockHistory VBA function and the OneDrive file synchronization process in Microsoft 365. To prevent irreversible data loss, you must move your testing to an isolated environment.

1
Create a Local Test Workbook

Save a copy of your XLSM or XLSB workbook locally on your computer (e.g., in a folder on your Desktop), completely outside the OneDrive sync directory.

2
Reproduce the Issue Safely

Open the local test workbook and run the macro containing the Application.StockHistory function to determine if the corruption still occurs without OneDrive synchronization.

3
Report to Microsoft Q&A

Navigate to the Excel Management section of Microsoft Q&A. Create a new thread detailing the issue, including your Windows 11 version, Excel build number, and the specific VBA code used.

Isolate the Macro and Report to Microsoft
Version History Recovery: If your file is already corrupted, log into OneDrive online, locate your Excel file, right-click, and select 'Version history' to restore a previous working version.
Free Microsoft Office alternative

Try WPS Office for Stable Spreadsheet Management

If you are encountering unresolvable VBA bugs and file corruption in Microsoft Excel, WPS Office offers a highly compatible, lightweight, and free alternative. It provides a stable environment for your daily spreadsheet tasks and macros without unexpected sync corruptions.

  1. 1. Download WPS Office: Visit the official WPS Office website and download the free installer for Windows.
  2. 2. Open Your Workbooks: Launch WPS Spreadsheet and open your existing XLSM or XLSB files without losing formatting or data.
  3. 3. Enable Macros: Ensure that macros are enabled in the WPS settings to continue working with your VBA scripts seamlessly.
Excellent compatibility with Microsoft Excel formats (.xlsx, .xlsm, .xlsb, .csv).Lightweight architecture ensures fast performance even with complex macro-enabled files.Familiar user interface makes transitioning from Microsoft Office completely seamless.Free to download and use for everyday office requirements.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Application.StockHistory corrupt my Excel file on OneDrive?

This is a known bug in Microsoft 365 Excel where the specific VBA function interacts poorly with OneDrive's continuous sync mechanism, causing save failures and subsequent file corruption.

How can I recover an XLSM file corrupted by this macro?

You can attempt to repair the file by launching Excel, going to File > Open, selecting the corrupted file, clicking the arrow next to the Open button, and choosing 'Open and Repair'. Alternatively, restore a previous version using OneDrive's Version History.

Does this issue happen if I save the file to a local drive?

The issue is primarily reported when workbooks are saved directly in OneDrive sync folders. Saving and running the macro in a completely local directory on your PC may prevent the corruption.