Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a VBA function in Excel return a range?

Tags:

excel

vba

I seem to be getting a type mismatch error when trying to do something like this:

In new workbook:

A1 B1 5  4  Function Test1() As Integer     Dim rg As Range     Set rg = Test2()     Test1 = rg.Cells(1, 1).Value End Function Function Test2() As Range     Dim rg As Range     Set rg = Range("A1:B1")     Test2 = rg End Function 

Adding =Test1() should return 5 but the code seems to terminate when returning a range from test2(). Is it possible to return a range?

like image 230
Dane O'Connor Avatar asked Jan 13 '09 15:01

Dane O'Connor


People also ask

Can a VBA function return an array?

A function in a normal module (but not a Class module) can return an array by putting () after the data type. Note that what is returned is actually a copy of the array inside the function, not a reference. So if the function returns the contents of a Static array its data can't be changed by the calling procedure.

How do you pass a range in a function in VBA Excel?

To allow for a variable number of ranges to be used in the function, you need to declare a ParamArray variant array in your argument list. Then, you can process each of the ranges in the array in turn. This function could then be used in the worksheet to add multiple ranges.

What does range return in VBA?

Excel VBA Range Object For example, the range property in VBA is used to refer to specific rows or columns while writing the code. The code “Range(“A1:A5”). Value=2” returns the number 2 in the range A1:A5.

How do you return a value from a function in Excel VBA?

To return a value using the Return statementPut a Return statement at the point where the procedure's task is completed. Follow the Return keyword with an expression that yields the value you want to return to the calling code. You can have more than one Return statement in the same procedure.


2 Answers

A range is an object. Assigning objects requires the use of the SET keyword, and looks like you forgot one in your Test2 function:

Function Test1() As Integer     Dim rg As Range     Set rg = Test2()     Test1 = rg.Cells(1, 1).Value End Function  Function Test2() As Range     Dim rg As Range     Set rg = Range("A1:B1")     Set Test2 = rg         '<-- Don't forget the SET here' End Function 
like image 173
BradC Avatar answered Sep 23 '22 08:09

BradC


You can also return a Variant() which represents an array of values. Here is an example for a function that reverses values from a range into a new range:

Public Function ReverseValues(ByRef r_values As Range) As Variant()     Dim i As Integer, j As Integer, N As Integer, M As Integer     Dim y() As Variant     N = r_values.Rows.Count     M = r_values.Columns.Count     y = r_values.value    'copy values from sheet into an array     'y now is a Variant(1 to N, 1 to M)      Dim t as Variant     For i = 1 To N / 2         For j = 1 To M             t = y(i, j)             y(i, j) = y(N - i + 1, j)             y(N - i + 1, j) = t         Next j     Next i      ReverseValues = y End Function 

In the worksheet you have to apply this function as an array formula (with Ctrl-Shift-Enter) with an appropriate number of cells selected. The details of the Swap() function are not important here.

Note that for many rows, this is very efficient. Doing the x = Range.Value and Range.Value = x operations when x is an array and the range contains multiple rows columns is many times faster than doing the operations one by one directly on the cells.

like image 35
John Alexiou Avatar answered Sep 21 '22 08:09

John Alexiou