How to Fix PtrSafe VBA Macro Crashing Excel on 64-Bit Systems
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.
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.
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.
Press ALT + F11 in Excel to open the Visual Basic for Applications (VBA) editor.
Search for the 'Declare PtrSafe' statements at the top of your code modules.
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.
Click on 'Debug' in the top menu and select 'Compile VBAProject' to verify there are no data type mismatch errors.
Verify Missing VBA References
Missing object library references on a specific machine can cause the macro to fail fatally upon execution.
Isolate the API Call with Error Handling
Test the API call independently to determine if a specific Windows security policy or antivirus software is blocking the execution.
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. Download WPS Office: Visit the official WPS website and download the free installation package.
- 2. Install and Launch: Follow the on-screen instructions to install the software, then open WPS Spreadsheets.
- 3. Open Your Files: Directly open your existing Excel workbooks and continue your data management smoothly.

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.




