> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Five Ways to Turn Runtime Errors into Compile Errors in VBA
- URL: https://nolongerset.com/turn-runtime-errors-into-compile-errors/
- Published: 2023-04-20T03:59:00.000Z
- Updated: 2026-05-08T12:50:24.000Z
- Description: Compile errors are cheaper and easier to fix than runtime errors. Here are five ways to turn potential runtime errors into compile errors.
- Author: Mike Wolfe
- Tags: Defensive Programming, Bug Types, VBA, #Import 2026-05-20 02:59

Compile errors are better than runtime errors.

[Some Bugs are Better than OthersNot all bugs are created equal. Avoid the expensive ones by making more of the ones that are easy to find and fix.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/car-2122580_1920.jpg)](https://nolongerset.com/some-bugs-are-better-than-others/)

That's all well and good, but it leads to an obvious follow up question. How does one turn a runtime error into a compile error?

Here is a list of techniques you can use to turn a runtime error into a compile error:

1. Use `Option Explicit`
2. Avoid `Variant` and `Object` types
3. Use constants in place of literals
4. Avoid "stringly-typed" code
5. Use early binding rather than late binding

### Use Option Explicit

When you add `Option Explicit` to the top of your code module, the compiler will require that you declare every variable before using it. This is a Good Thing™ because it means that you can worry less about typos when naming and using your variables. 

[Why You Should Always Use Option Explicit in VBADon’t let typos and logic errors ruin your VBA code. Read our latest blog post to learn about the importance of Option Explicit and how to use it.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2023/04/Use-Option-Explicit.jpeg)](https://nolongerset.com/use-option-explicit/)

### Avoid Variant and Object Types

These "catch-all" data types tell you nothing about the values that they contain. They also tell the *compiler* nothing about the values they contain. And that means the compiler can't perform type checking to help keep you from shooting yourself in the foot.

[Strongly Typed Collections in VBAGet better type safety and access to IntelliSense inside For Each loops by using “strongly-typed collections” in place of the VBA Collection type.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/scenic-3399836_1920.jpg)](https://nolongerset.com/strongly-typed-collections-in-vba/)

### Use Constants in Place of Literals

Not only do constants help you write self-documenting code, they also help you avoid errors from typos in your code.

[How to Construct a Bit Mask in VBAWhat’s the safest way to construct a bit mask in VBA? Your preferred approach may be more dangerous than you realize.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/08/venetian-mask-1283163_1920.jpg)](https://nolongerset.com/how-to-construct-a-bit-mask-in-vba/#magic-numbers-the-worst-approach)

Pay particular attention to the sections on [magic numbers](https://en.wikipedia.org/wiki/Magic%5Fnumber%5F%28programming%29) and the "Addition of Constants."

### Avoid "Stringly-Typed" Code

Scott Hanselman discussed this topic in a [blog post](https://www.hanselman.com/blog/stringly-typed-vs-strongly-typed) several years ago:

> When you are using a strongly typed language but instead your types are *stringly typed,* you are passing strings around when a better type exists.

Here's an example of how you can turn "stringly-typed" OpenArgs values into *strongly-typed* values using Data Transfer Objects in VBA:

[“Complex” DTOs in VBACan you use the OpenArgs parameter to pass multiple values to forms and reports with compile-time checking? You can if you use DTOs.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2020/12/NLS---Complex-DTOs-in-VBA.jpg)](https://nolongerset.com/complex-dtos-in-vba/)

### Use Early Binding rather than Late Binding

The standard advice when it comes to early and late binding is that you should develop using early binding but deploy using late binding. In fact, [I provide some of that advice myself](https://nolongerset.com/early-binding-vs-late-binding/)! However, as I list at the bottom of the linked article, there are some real disadvantages to using late binding.

> When you rely on late binding in your VBA code, you give up four big advantages:  
> • Type safety  
> • IntelliSense  
> • Named constants  
> • Event handling

My existing advice still holds for automating Microsoft Office products (to avoid type library version compatibility issues with Excel, for instance). That said, I have started deploying apps with early binding left in place for very stable libraries that I know will be available on the end users' computers, including:

- Microsoft Scripting Runtime
- Microsoft VBScript Regular Expressions 5.5