To use Kernel32 functions such as Sleep, GetTickCount, or memory APIs from Excel VBA on 64-bit Office, you must declare them with the PtrSafe keyword and use LongPtr for pointer-sized arguments. The old 32-bit declarations that used Long for handles will compile but crash or return wrong values on 64-bit Excel. Below is the exact process to convert and test your declarations safely.

What You'll Need

  • Excel 2010 or later (64-bit Office requires VBA7; Excel 2007 and earlier are 32-bit only).
  • A workbook saved as .xlsm or .xlsb so macros persist.
  • Access to the VBA editor (Alt+F11) and the Trust Center setting that allows macros to run.
  • A text editor or notes app to keep a copy of the original declarations before editing.

Step-by-Step

  1. Open the VBA editor and locate the declaration. Press Alt+F11, then use Ctrl+F to search for Declare or Lib "kernel32". Note the function name and its argument types. If you see Declare Function without PtrSafe, it is a 32-bit-only declaration.
  2. Add the conditional compilation constant. At the very top of the module, before any declarations, insert:
    #If VBA7 Then
    This constant is True in Office 2010 and later, including both 32-bit and 64-bit builds, and False in older versions.
  3. Rewrite the declaration with PtrSafe and LongPtr. Replace the old line with a PtrSafe version. For example, change:
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    to:
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    For functions that return or accept handles, use LongPtr instead of Long. Example: Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long is fine because the return is a 32-bit DWORD, but GetCurrentProcess should return LongPtr.
  4. Close the conditional block. After the PtrSafe declarations, add an #Else section containing the original 32-bit declarations, then #End If. This keeps the code working in Excel 2007 and earlier. If you only target Office 2010+, you can omit the Else branch, but keeping it is safer for shared workbooks.
  5. Update variable types in your procedures. Any variable that receives a handle or pointer from a Kernel32 call must be declared as LongPtr, not Long. Search your code for assignments from those functions and change the receiving variable type. Also change API constants that represent pointer-sized values to LongPtr where appropriate.
  6. Compile the project. In the VBA editor, choose Debug > Compile VBAProject. Fix any “Type mismatch” or “Expected: type name” errors. A common fix is replacing As Long with As LongPtr in the declaration and in the calling code.
  7. Test on both bitnesses if possible. Run the macro in 64-bit Excel. If you have access to 32-bit Excel, open the same workbook and run it again. The conditional compilation should select the correct declaration automatically. Check that return values are not truncated (for example, a handle that appears as a negative number indicates a missing LongPtr).
  8. Save and document. Save the workbook as .xlsm. Add a comment above the declarations noting that the code uses VBA7 conditional compilation and LongPtr for 64-bit compatibility.

Common Mistakes

  • Using Long for handles on 64-bit Office. Handles are 8 bytes on 64-bit Windows; Long is 4 bytes, so values are truncated and calls fail.
  • Forgetting PtrSafe on any declaration. Without it, 64-bit Excel raises a compile error or refuses to run the macro.
  • Placing #If VBA7 after the declarations instead of before. Conditional compilation must wrap the declarations themselves.
  • Mixing 32-bit and 64-bit declarations in the same branch. Each branch must be complete and consistent.
  • Assuming all Kernel32 functions need LongPtr. Functions that take or return a DWORD (32-bit) still use Long; only pointer-sized values use LongPtr.

Tips

  • Use the LongPtr type consistently in both the declaration and the calling procedure. A mismatch causes “Type mismatch” at runtime.
  • Keep a copy of the original 32-bit declarations in the #Else branch so the workbook still works in older Excel versions.
  • If you only need simple timing, consider VBA's built-in Timer function instead of Kernel32 calls; it avoids declaration issues entirely.
  • Test with Debug.Print on returned handles to confirm they are not negative or zero.
  • When sharing the workbook, note in the code comments that it requires Office 2010 or later for the VBA7 branch.

Troubleshooting

If you get a compile error “The code in this project must be updated for use on 64-bit systems,” a declaration is missing PtrSafe. Search the module for Declare and add PtrSafe to every one. If the error persists, check that the #If VBA7 block encloses all declarations.

If the macro runs but returns wrong values, check for Long variables receiving pointer-sized returns. Change them to LongPtr. If a handle appears as a negative number, it has been truncated to 32 bits.

If the macro works in 32-bit Excel but fails in 64-bit, confirm that the #Else branch contains the original declarations and that the #If VBA7 constant is spelled correctly. Also check for any hard-coded Long types in the procedure that call the API.

If Excel crashes when calling a Kernel32 function, verify the argument types match the API signature exactly. For example, Sleep takes a Long (DWORD), not a LongPtr. Use the official Microsoft documentation for each function's signature.

Frequently Asked Questions

Do I need PtrSafe for 32-bit Excel?

No. PtrSafe is ignored by 32-bit Excel, but including it is harmless and keeps the code compatible with both bitnesses when wrapped in #If VBA7.

What is the difference between Long and LongPtr?

Long is always 32 bits. LongPtr is 32 bits in 32-bit Office and 64 bits in 64-bit Office, so it matches the pointer size of the host process.

Can I use Kernel32 calls in Excel for Mac?

No. Kernel32 is a Windows API; Mac Excel does not provide it. Use VBA's built-in functions or AppleScript alternatives instead.

Why does my declaration still fail after adding PtrSafe?

Check that every argument that represents a handle or pointer uses LongPtr, and that the calling code uses the same type. A single Long in the wrong place can cause a type mismatch.

Is it safe to call Kernel32 functions from VBA?

It is safe if you use correct signatures and avoid functions that modify memory or system state. Stick to well-documented functions like Sleep and GetTickCount.

Do I need to change my code if I move from 32-bit to 64-bit Office?

Yes. Any API declaration without PtrSafe must be updated, and pointer-sized variables must become LongPtr. The conditional compilation approach shown above handles both.