This check allows your VBA code to detect if it is running on a 64-bit version of Excel, specifically when using the VBA7 runtime. You need to place the VBA7 runtime on a machine running a 64-bit version of Windows. The code then tells your script to use 64-bit compatible data types and API declarations when that environment is detected, preventing runtime errors.
What You'll Need
- A computer running a 64-bit version of Microsoft Windows.
- A copy of Microsoft Excel installed on that machine.
- The Visual Basic for Applications (VBA) editor, accessible via the Developer tab or by pressing Alt + F11.
- A working knowledge of basic VBA syntax, specifically how to declare variables and write conditional logic.
Step-by-Step
- Open the VBA Editor. Launch Excel and press Alt + F11 to open the Visual Basic for Applications editor window. If the Developer tab is visible in the ribbon, you can also right-click any worksheet tab, select View Code, or use the Developer tab to open the editor.
- Insert a Module. In the VBA editor, go to the menu bar and click Insert. From the dropdown list, select Module. This creates a new code module where you can write your macros.
- Check the VBA Project Properties. In the top-left pane of the editor, locate your VBA project name (usually the name of your Excel file). Right-click on it and select Properties. In the Properties window that appears, look at the Help File Name or Help Context ID fields. If these fields are populated with numbers, your project is linked to the VBA7 runtime, which is required for 64-bit compatibility. If the fields are empty, you must link the runtime manually. To do this, go to the Tools menu in the VBA editor and select References. In the References dialog, scroll through the list and look for entries that say "Visual Basic For Applications" and "OLE Automation". Ensure the boxes next to these entries are checked. This ensures the VBA7 runtime is loaded.
- Write the Conditional Check. Click into the code window of your new module. Type the following code structure to begin your conditional check:
Sub CheckEnvironment()
If VBA7 And Win64 Then
' Your code here for 64-bit
Else
' Your code here for 32-bit or older
End If
End Sub
- Understand the Logic. The
IsVBA7function checks if the VBA7 runtime is present. TheWin64function checks if the operating system is 64-bit. TheAndoperator combines these two checks. The result isTrueonly if both conditions are met. This allows you to run different code paths based on the environment. - Implement 64-Bit Specific Code. Inside the
Ifblock, declare variables using theLongPtrdata type instead ofLong. This is crucial becauseLongPtris 4 bytes on 32-bit systems and 8 bytes on 64-bit systems. For example, declare a pointer to a window handle asDim hWnd As LongPtr. You can also declare API functions usingLongPtrfor their parameters. For example,Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long. - Implement 32-Bit or Legacy Code. Inside the
Elseblock, write your code for 32-bit systems. Use the standardLongdata type for variables and declare API functions without thePtrSafekeyword. For example,Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long. - Test Your Code. Save your VBA project and return to the Excel interface. Press Alt + F11 again to open the editor. Place a breakpoint on the line
If VBA7 And Win64 Thenby clicking in the gray margin to the left of the line. Press F5 to run the macro. When execution stops at the breakpoint, check the Immediate Window (Ctrl + G) to see the value ofVBA7andWin64. You should seeTruefor both if you are on a 64-bit system. If you are on a 32-bit system,VBA7will beFalse. - Save the Workbook. Once you are satisfied with your code, save the Excel workbook. Remember that VBA macros are embedded within the file, so the file size may increase slightly. You can save the file as an Excel Macro-Enabled Workbook (.xlsm) to preserve the macros.
Common Mistakes
One of the most common errors when writing cross-platform code is declaring API functions without the PtrSafe keyword. On a 64-bit system, this will cause a type mismatch error or a runtime error 458. Always use the PtrSafe keyword when declaring API functions inside the If VBA7 And Win64 Then block. Another mistake is using Long instead of LongPtr for variable declarations, which can lead to memory corruption or incorrect calculations on 64-bit systems. Always check the data type of your variables and API parameters.
Tips
When declaring API functions, it is good practice to use the PtrSafe keyword even on 32-bit systems. The PtrSafe keyword is ignored by 32-bit compilers, but it makes the code more readable and easier to maintain. It also helps to document your code by adding comments that explain the purpose of the conditional check. For example, ' This block of code is executed only on 64-bit systems. This makes it easier for other developers to understand your code later.
Troubleshooting
If you encounter a runtime error 458, it usually means you are trying to use a VBA7-specific feature, such as PtrSafe, on a system that does not have the VBA7 runtime installed. To fix this, ensure that the VBA7 runtime is linked to your project by checking the References dialog. If the reference is missing, add it manually. If you are still seeing errors, check the Immediate Window to see the values of VBA7 and Win64. If VBA7 is False, your project is not linked to the VBA7 runtime, and you need to add the reference. If Win64 is False, you are running on a 32-bit system, and you should be using the code in the Else block.
Frequently Asked Questions
What is the difference between VBA7 and the VBA runtime?
VBA7 is the version of the runtime that includes support for 64-bit applications. The standard VBA runtime is designed for 32-bit applications. The VBA7 runtime is automatically installed on 64-bit systems, but you must link it to your project in the VBA editor.
Why do I need to use LongPtr instead of Long?
LongPtr is a data type that automatically adjusts to the size of the system. On a 32-bit system, it is 4 bytes, and on a 64-bit system, it is 8 bytes. Using Long can lead to errors on a 64-bit system because the data type is too small to hold the value needed.
Can I use the PtrSafe keyword on a 32-bit system?
Yes, you can use the PtrSafe keyword on a 32-bit system. The PtrSafe keyword is ignored by 32-bit compilers, so it will not cause any errors. It is a good practice to use it because it makes the code more readable and easier to maintain.
What happens if I don't use the conditional check?
If you don't use the conditional check, your code will likely fail on a 64-bit system if you are using 32-bit-specific code. For example, if you declare an API function without the PtrSafe keyword, it will cause a runtime error 458 on a 64-bit system.
How do I know if my Excel is 64-bit or 32-bit?
You can check the version of your Excel by going to File > Account > About Excel. The version number will indicate whether you are using 64-bit or 32-bit. For example, if the version number starts with 16.0, you are likely using 64-bit Excel, but you should verify the build number to be sure.
Can I use this check for other applications besides Excel?
This check is specific to the VBA environment. If you are using other applications that support VBA, such as Microsoft Word or Access, you can use the same check to ensure your code is compatible with the platform.