Pages

How to Test a Custom VBA Function in Immediate Window

In this post I will show you how you can test a custom function in the immediate window. When you develop advanced VBA applications with multiple subroutines and functions you may need to test them while developing the code. Then you can use this technique to test individual functions without running the whole program. So now let’s look at how to call a user defined function from the immediate window. Here is a simple custom function I created to use as an example.

Function SumValues()

     Dim a As Long
     Dim b As Long
     Dim c As Long
     Dim Result As Long

     a = 1000
     b = 2000
     c = 3500

     Result = a + b + c
     SumValues = Result

End Function

This is a very simple function which returns the sum of three values. Now let’s see how we can test this function in the immediate window. First open the immediate window if it is not already opened.

How to Show Immediate Window in VBA Environment

When you open the immediate window you can test the custom function using one of the below two methods.

Method 1

Enter the function name followed by a question mark.

?SumValues

So here is the result you will get.

Call a custom function in the immediate window

That's one way to call a custom function from the immediate window.

Method 2

In this method we are going to use the debug.print method to print the result of the function in the immediate window.

This is how you can test a custom VBA function in the immediate window using this second method.

debug.Print SumValues()
Test function in the immediate window using debug.print method

Also read
How to Return an Array From VBA Function
How to Pass an Array to a VBA Function
How to Search For Multiple Strings Using VBA InStr Function
BESSELJ Function