Learn how to return values based on where you enter the UDF
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
- Press with left mouse button on "Insert" on the menu
- Press with left mouse button on "Module"
- Copy/Paste vba code to code module
- Return to excel
Get excel *.xlsm file
This article demonstrates formulas that count values in cell based on a delimiting character. The image above shows a formula […]
This article demonstrates a User Defined Function (UDF) that counts unique distinct cell values based on a given cell color. […]
This article describes how to count unique distinct values in list. What is a unique distinct list? Merge all duplicates to one […]
Blake asks: I have a somewhat related question, if you don't mind: I have very large amount of text in […]
This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]
AJ Serrano asks: I have a column where each rows contains different values and I wanted to obtain the duplicate […]
This article demonstrates a user defined function that extracts duplicate values and also count duplicates. Example, the image below shows a list containing […]
This post describes a custom function (User defined Function) that extract values existing only in one out of two cell […]
The User Defined Function demonstrated above extracts unique distinct records also considering upper and lower case letters. For example, a record […]
This blog post describes how to create a list of unique distinct words from a cell range. Unique distinct words […]
This blog post describes how to create a list of unique words from a cell range. Unique words are all […]
The image above demonstrates a User Defined Function that extracts all words containing a given string that you can specify. In […]
This article describes how to find a sum from a range of numbers using a user defined function. Let´s see […]
Question:I would like to reduce the number of open items by identifying positive and negative amounts that net to zero […]
In this post I will describe a basic user defined function with better search functionality than the array formula in […]
This user defined function creates a unique distinct list of words and how many times they occur in the selected […]
Macros and custom functions are great, they can automate many tedious tasks. To have them available whenever you need them, […]
This article demonstrates a user defined function that lists files in a ggiven folder and subfolders. A user defined function is […]
This blog post describes how to create permutations, repetition is NOT allowed. Permutations are items arranged in a given order meaning […]
This blog post demonstrates a custom function (UDF) that creates permutations. Repetition is allowed. The custom function lets you specify the […]
This article explains how to perform multiple lookups based on values in one cell with a delimiting character using a […]
Sean asks: Sheet1A B C D 8 Country Europe 9 Lights 100 10 Type A 200 11 12 Country USA […]
The image above demonstrates a user-defined function in cell range B6:D7 that allows you to search a folder and subfolders […]
This article demonstrates formulas and a UDF that searches for values in a table based on concatenated values and returns […]
Question: How do I divide values equally into groups (3 lists or less)? This post shows you two different approaches, […]
This post describes how to split words in a cell range into a cell each using a custom function. I […]
The SUBSTITUTE and REPLACE functions can only handle one string, the following User-Defined Function (UDF) allows you to substitute multiple […]
This post describes a User Defined Function that searches multiple ranges and adds corresponding values across worksheets in a workbook. A […]
This article describes a User Defined Function that lookups values in multiple cross reference tables based on two conditions. A […]
4 Responses to “Learn how to return values based on where you enter the UDF”
Leave a Reply to Rick Rothstein (MVP - Excel)
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
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...
Rick 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
https://fastexcel.wordpress.com/