Learn how to return values depending on udf deployment (vba)
In this vba tutorial I am going to show you how to return values from an udf, depending on where you enter the udf. All udf examples in this post remove blanks but they differ on how values are returned.
Example 1
The values are returned horizontally and #N/A are returned when there are no more values to show.
User defined function
Function BasicUDF(Rng As Range) Dim Cell As Variant 'Create an array Dim temp() As Variant ReDim temp(0) 'Process every cell in Range For Each Cell In Rng 'Check if cell is empty If Cell <> "" Then 'Copy cell value to the last position in array temp(UBound(temp)) = Cell 'Increase array size with 1 ReDim Preserve temp(UBound(temp) + 1) End If Next Cell 'Remove the last value in array ReDim Preserve temp(UBound(temp) - 1) 'Return values BasicUDF = temp End Function
You can return values vertically by changing the last line to BasicUDF = Application.Transpose(temp). See animated gif below.
Values are transposed vertically.
Example 2
This user defined function returns values vertically and replaces #N/A with blanks.
Application.Caller.Rows.Count returns the number of rows from the cell range where you entered the udf. This makes it possible to add blanks when the udf is out of values.
User defined function
Function BasicUDF2(Rng As Range)
Dim Cell As Variant
Dim row As Single
'Create an array
Dim temp() As Variant
ReDim temp(0)
'Process every cell in Range
For Each Cell In Rng
'Check if cell is empty
If Cell <> "" Then
'Copy cell value to the last position in array
temp(UBound(temp)) = Cell
'Increase array size with 1
ReDim Preserve temp(UBound(temp) + 1)
End If
Next Cell
'Add remaining blanks to array
For row = UBound(temp) To Application.Caller.Rows.Count
temp(UBound(temp)) = ""
ReDim Preserve temp(UBound(temp) + 1)
Next row
'Remove last blank
ReDim Preserve temp(UBound(temp) - 1)
'Transpose temp array from horizontal to vertical and return values to sheet
BasicUDF2 = Application.Transpose(temp)
End Function
Example 3
This user defined function returns values in every cell, also if you entered the user defined function in multiple columns. The remaining cells are returned as blanks. See animated gif.
Values are transposed to fill the entire selection, #N/A are replaced with blank cells.
User defined function
Function BasicUDF3(Rng As Range)
Dim Cell As Variant
Dim row As Single, i As Single
'Create an array
Dim temp() As Variant
'Dim array with same size as selection
ReDim temp(Application.Caller.Columns.Count - 1, 0)
i = 0
'Process every cell in Range
For Each Cell In Rng
'Check if cell is empty
If Cell <> "" Then
'Copy cell value to the last position in array
temp(i, UBound(temp, 2)) = Cell
i = i + 1
'Add a new row to the array
If i = Application.Caller.Columns.Count Then
i = 0
ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) + 1)
End If
End If
Next Cell
'Process remaining cells in selection
Do
'Remaining value are blanks
temp(i, UBound(temp, 2)) = ""
'Count current column
i = i + 1
'Check if current column is equal to the number of columns in selection
If i = Application.Caller.Columns.Count Then
'Start over at column 1 (0)
i = 0
'Add a new row in array
ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) + 1)
End If
Loop Until UBound(temp, 2) > Application.Caller.Rows.Count - 1
'Remove last row in array
ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) - 1)
'Return values
BasicUDF3 = Application.Transpose(temp)
End Function
How to create an array formula
- Select a cell range
- Type user defined function in formula bar
- Press and hold Ctrl + Shift
- Press Enter
Where do I copy the vba code?
- Open VB Editor or press Alt+ F11
- Click "Insert" on the menu
- Click "Module"
- Copy/Paste vba code to code module
- Return to excel
Download excel *.xlsm file
Related posts:
Excel udf: Filter unique distinct values (case sensitive)
Learn how to create a stock chart in excel
Excel udf: Combine cell ranges into a single range while eliminating blanks





















Here is an extra-credit challenge for your readers. Once this article makes sense to them, they should try to figure out how this shorter UDF (which does what your basicUDF3 does) works...
Function BasicUDF3(Rng As Range) As Variant Dim ArrIn As Variant, ArrOut() As Variant, V As Variant Dim X As Long, Z As Long, Item As Long, Rw As Long, Col As Long ArrIn = Rng.Value ReDim ArrOut(1 To Application.Caller.Rows.Count, _ 1 To Application.Caller.Columns.Count) For Each V In ArrIn If Len(V) Then Item = Item + 1 Rw = 1 + Int((Item - 1) / UBound(ArrOut, 2)) Col = 1 + ((Item - 1) Mod UBound(ArrOut, 2)) ArrOut(Rw, Col) = V End If Next For X = 1 To UBound(ArrOut) For Z = 1 To UBound(ArrOut, 2) If IsEmpty(ArrOut(X, Z)) Then ArrOut(X, Z) = "" Next Next BasicUDF3 = ArrOut End FunctionRick Rothstein (MVP - Excel),
I am going to change this post, it is clear as mud!
Thanks for your contribution!
Hi. I am trying to make it very fast for a UDF to return its original value under certain conditions. I created a test Excel file that has close to 60,000 cells that will either return the current time or return the original caller value.
If I don't access the caller value it will calculate in 6 seconds. (About 4 seconds if I am not in debug mode). But if I access the caller value, it takes 4 to 5 times longer.
I was hoping there was another way to return the caller value that would be faster. If it's not an easy answer, I was wondering if I bought a higher version of ADX with the source code, are there any opportunities to adjust the source code to accomplish this? Speed is really everything for what I am trying to accomplish.
I am using an XLL/XLA C# project. I am open to other methods to accomplish the goal.
I can send you my test project if needed. I would have attached, but I don't know how to attach to this post.
Thanks for your help.
Dwipayan Das,
Contact Charles Williams
http://fastexcel.wordpress.com/