logo
search
VBA & Macro Problems

How to Fix PtrSafe VBA Macro Crashing Excel on 64-Bit Systems

Maira MehtabMaira Mehtab Sep 20, 2026 869 views

Question details

The user is experiencing an issue where a VBA macro containing PtrSafe API declarations works normally on some 64-bit computers but causes Microsoft Excel to abruptly close or trigger a repair process on others.

Product
Microsoft Excel
Device & OS
64-bit Windows
Scenario
Running a 64-bit compatible VBA macro containing Windows API calls across different computer environments.
Observed behavior
Excel unexpectedly closes or initiates an automatic file repair process on specific machines instead of executing the intended macro.
Before you start

Ensure you have access to the unprotected VBA code module and verify the exact Office architecture (32-bit vs. 64-bit) installed on the specific computer that is experiencing the crash.

Solution 1Recommended

Update API Declarations with LongPtr Data Types

Use this solution to ensure that memory pointers are correctly sized for 64-bit environments, preventing memory corruption that leads to crashes.

In 64-bit VBA, simply adding the 'PtrSafe' keyword is not sufficient. You must also update the variable data types for any handles or pointers to avoid memory address truncation.

1
Open the VBA Editor

Press ALT + F11 in Excel to open the Visual Basic for Applications (VBA) editor.

2
Locate API Declarations

Search for the 'Declare PtrSafe' statements at the top of your code modules.

3
Update Variable Types

Change any 'Long' data types to 'LongPtr' if they represent memory pointers or window handles (e.g., hWnd, wParam). Use 'LongLong' strictly for 64-bit integers.

4
Compile the VBA Project

Click on 'Debug' in the top menu and select 'Compile VBAProject' to verify there are no data type mismatch errors.

Best Practice: Use conditional compilation (#If Win64 Then) to safely maintain code compatibility between 32-bit and 64-bit Excel versions.
Free Microsoft Office alternative

Try WPS Office for Stable Spreadsheet Management

If Microsoft Excel continues to crash or struggle with macro execution across different environments, consider switching to WPS Office. It provides a lightweight, highly compatible platform for managing your spreadsheets with a familiar interface, ensuring a seamless migration without heavy system resource usage.

  1. 1. Download WPS Office: Visit the official WPS website and download the free installation package.
  2. 2. Install and Launch: Follow the on-screen instructions to install the software, then open WPS Spreadsheets.
  3. 3. Open Your Files: Directly open your existing Excel workbooks and continue your data management smoothly.
Free and lightweight alternative to Microsoft ExcelHighly compatible with Microsoft Office formats like .xlsx and .xlsmFamiliar user interface for a seamless transition without a steep learning curveStable performance across various Windows system configurations
microsoft office alternative - wps office

Frequently Asked Questions

What does the PtrSafe keyword do in Excel VBA?

PtrSafe is a keyword introduced in VBA7 that informs the compiler that the API Declare statement is safe to run in 64-bit versions of Office. However, it does not automatically resize 32-bit pointers to 64-bit; you must manually update pointer variables to the LongPtr data type.

Why does LongPtr matter in 64-bit VBA?

In 64-bit systems, memory addresses (pointers) require 64 bits of storage. Using a standard 32-bit 'Long' data type for a pointer truncates the memory address, leading to memory corruption and causing Excel to crash.

How can I make my macro work on both 32-bit and 64-bit Excel?

You can use conditional compilation. Use '#If Win64 Then' or '#If VBA7 Then' followed by your 64-bit API declarations using PtrSafe and LongPtr, and use '#Else' for the older 32-bit declarations.

Can antivirus software cause a VBA macro to crash Excel?

Yes. Some strict antivirus or endpoint protection policies monitor Windows API calls. If a macro attempts to access system processes using API calls, the security software may forcefully terminate Excel to protect the system.