Fix Excel VBA Compile Errors in 64-Bit Office
Question details
Resolve a VBA compile error requiring 64-bit-compatible Declare statements in an Excel workbook.

- Product
- Microsoft Excel for Microsoft 365
- Device & OS
- Windows 10
- Scenario
- A workbook containing VBA code works on some computers but fails to compile on another.
- Observed behavior
- Excel reports that the VBA project must be updated for 64-bit systems and that Declare statements require the PtrSafe attribute.
In Excel, open File > Account > About Excel to check whether Office is 32-bit or 64-bit; check the same setting on a computer where the workbook works. Make a local copy of the workbook before editing its VBA code.
Update VBA API declarations for 64-bit Office
Use this solution when the compiler identifies a Declare statement that is not compatible with 64-bit Office.
The PtrSafe attribute marks a declaration as compatible with 64-bit Office, but adding it alone does not make incorrect parameter or return types safe. Windows handles and pointers generally require LongPtr in VBA7; ordinary 32-bit integer values may still require Long.
Open the local workbook copy in Excel and press Alt+F11. If the project is password-protected, contact its owner for access.
In the VBA editor, select Debug > Compile VBAProject. Inspect the highlighted Declare statement and other API declarations in the project.
Add PtrSafe to declarations used in VBA7 and review each parameter and return type against the API documentation. Use LongPtr for pointer-sized values where appropriate; do not replace every Long automatically.
If the workbook must also run in pre-VBA7 Office, use conditional compilation with #If VBA7 Then for the PtrSafe declaration and #Else for a compatible older declaration.
Select Debug > Compile VBAProject again, correct any remaining errors, and run the affected macro on the target computer. Have a qualified VBA developer or IT administrator review changes to API declarations.

Update Office and Windows before retesting the workbook
Use this check to rule out outdated software as a contributing factor; updates do not replace the need to correct incompatible VBA declarations.
Work with Excel files in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday workbook tasks. It does not fix Microsoft Excel's VBA compile error; check macro compatibility before migrating VBA-dependent files.

Frequently Asked Questions
Why does the workbook work on one computer but not another?
The computers may have different Office bitness or VBA environments. In Excel, check File > Account > About Excel on each computer and compare whether Office is 32-bit or 64-bit.
Is adding PtrSafe enough to fix every Declare statement?
No. PtrSafe indicates 64-bit compatibility, but pointer and handle parameters and return values must also use appropriate types, often LongPtr in VBA7. Review the API signature before changing the code.
Should I replace every Long with LongPtr?
No. Use LongPtr for values that represent pointers or handles when the API signature requires it. Ordinary 32-bit integer values may still need Long.
How can I check for more VBA compile errors?
Press Alt+F11 in Excel to open the VBA editor, then select Debug > Compile VBAProject. Address the highlighted errors and compile again.
Can the same declarations support older Office versions?
Yes, when written for the relevant environments. Use #If VBA7 Then to include VBA7 declarations with PtrSafe and LongPtr, and #Else for declarations compatible with older VBA versions.




