VBA & Macro Problems

Fix Excel VBA Compile Errors in 64-Bit Office

Olivia MillerOlivia Miller Nov 18, 2026 869 views

Question details

Resolve a VBA compile error requiring 64-bit-compatible Declare statements in an Excel workbook.

Fix Excel VBA Compile Errors in 64-Bit Office
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

Open the local workbook copy in Excel and press Alt+F11. If the project is password-protected, contact its owner for access.

2
Find the declaration

In the VBA editor, select Debug > Compile VBAProject. Inspect the highlighted Declare statement and other API declarations in the project.

3
Update the signature

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.

4
Handle older VBA versions

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.

5
Compile and test

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 VBA API declarations for 64-bit Office
Office bitness matters: A 64-bit Windows installation can run either 32-bit or 64-bit Office. Differences in Office bitness can explain why the same workbook behaves differently across computers.
Free Microsoft Office alternative

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.

Open and edit common Excel formats, including XLSX and XLSUse a familiar spreadsheet interface for everyday workbook tasksMove compatible spreadsheets between Office applicationsUse a free, lightweight office suite
microsoft office alternative - wps office

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.