logo
search
VBA & Macro Problems

How to Fix a VBA Macro That Crashes 64-Bit Excel

Kushani NimanthikaKushani Nimanthika Sep 30, 2026 869 views

Question details

A VBA macro originally designed for 32-bit Excel crashes unexpectedly when run on 64-bit Microsoft 365, despite having been updated with PtrSafe and LongPtr.

How to Fix a VBA Macro That Crashes 64-Bit Excel
Product
Microsoft Excel / Microsoft 365
Device & OS
Windows
Scenario
Adapting a 32-bit VBA macro to run on a 64-bit Excel environment.
Observed behavior
Excel unexpectedly closes or crashes when executing the modified macro, likely due to improperly typed Windows API calls.
Before you start

Before modifying your VBA code, ensure you have saved a backup copy of your workbook. Incorrect API declarations can cause Excel to crash without saving your latest changes.

Solution 1Recommended

Correct the Windows API Return Types for 64-bit Excel

Properly declare Windows API functions by ensuring only pointers and handles use LongPtr, while standard numerical or Boolean returns remain as Long.

When adapting macros from 32-bit to 64-bit Excel, a common mistake is indiscriminately changing all 'Long' variable types to 'LongPtr'. For functions like GetUserName, the return value represents a Boolean-style success flag, which must remain a standard Long variable. Over-allocating memory size for a return value by using LongPtr can lead to fatal application crashes in a 64-bit architecture.

1
Open the VBA Editor

Press Alt + F11 within your Excel workbook to launch the Visual Basic for Applications (VBA) Editor.

2
Locate API Declarations

Use the Project Explorer to find the module containing the GetUserName or other Windows API Declare statements.

3
Adjust the Return Type

Update the declaration to include the 'PtrSafe' keyword, but ensure the return type at the end of the statement is explicitly set to 'As Long' rather than 'As LongPtr'.

4
Verify Pointers and Handles

Check the parameters within the parentheses of the API call. Ensure that only variables representing actual memory pointers or window handles (like hWnd) are declared as 'LongPtr'.

5
Compile and Test

Go to Debug > Compile VBAProject to check for immediate syntax errors, then save the workbook and run the macro to verify stability.

Correct the Windows API Return Types for 64-bit Excel
Alternative Method: If you only need to retrieve the current Windows username, you can bypass complex API calls entirely by using the built-in VBA function: Environ("username").
Free Microsoft Office alternative

Try WPS Office for Stable Spreadsheet Macro Management

If you are tired of dealing with Excel crashes and complex 32-bit to 64-bit VBA migration issues, consider switching to WPS Office. It provides a lightweight, highly compatible environment with excellent macro support.

  1. 1. Download and Install: Download WPS Office for free from the official website and follow the installation wizard.
  2. 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the VBA macros.
  3. 3. Enable Macros: Click 'Enable Macros' when prompted at the top of the screen and run your scripts in a highly stable environment.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .xlsm).Robust macro support to run your existing VBA scripts smoothly without heavy system crashes.Free, lightweight, and consumes significantly fewer system resources.Familiar user interface requiring zero learning curve for long-time Excel users.
QA img-9

Frequently Asked Questions

What is the difference between Long and LongPtr in VBA?

In VBA, 'Long' is a strictly 32-bit integer used for standard numerical values. 'LongPtr' is a variable type that adapts to the environment—it acts as a 32-bit integer in 32-bit Office and a 64-bit integer in 64-bit Office, making it essential for safely handling memory pointers and handles.

Why does my macro work on some 64-bit computers but crash on others?

Differences in Windows builds, installed Office updates, or slightly different versions of the underlying Windows API libraries (DLLs) can cause varying behavior. Incorrect declarations might coincidentally align in memory on one machine but trigger a fatal memory access violation on another.

How do I easily get the Windows username without using API calls?

You can use the built-in VBA environment variable function by typing Environ("username"). This avoids complex Windows API declarations entirely and works seamlessly across both 32-bit and 64-bit Office versions.

Do I need to add PtrSafe to all my API declarations in 64-bit Excel?

Yes, in 64-bit Office versions, all Declare statements must include the PtrSafe keyword to indicate to the compiler that the declaration is safe to run in a 64-bit environment. However, you must also be precise about which specific variables inside the declaration are changed to LongPtr.