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?
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With