Pass multidimensional array into Excel UDF in VBA - excel

How do I pass multidimensional arrays or nested arrays into an Excel UDF which allow me to reference other ranges?
I have a UDF defined as "ARY" which does what Array() does in VBA but in a worksheet function.
This allows me to have a worksheet formula like
=TEST1(ARY(ARY("A", "B"), "C"))
or
=TEST1(ARY(ARY(A1, B1), C1)
However, I get Error 2015 when executing TEST1 as a worksheet function. If I execute TEST1 from VBA, it works fine and returns "A".
Public Function TEST1(Params As Variant) As Variant
TEST1 = Params(0)(0)
End Function
'Returns 1D ARRAY
Public Function ARY(ParamArray Params() As Variant)
ReDim result(0 To UBound(Params)) As Variant
Dim nextIndex As Integer
Dim p As Variant
nextIndex = 0
For Each p In Params
result(nextIndex) = p
nextIndex = nextIndex + 1
Next
ARY = result
End Function

In general, you can't do this. VBA is fine with nested arrays as values, but Excel just isn't. This makes sense, because Excel ultimately has to treat any values it gets from your UDFs as things that can go into the worksheet grid.
It would be nice if intermediate results didn't have to obey this restriction. In your example above, you're not trying to put a jagged array into any cells; you just want to create it and pass it to 'TEST1'. Unfortunately, Excel just doesn't work that way. It evaluates each expression in a formula to a legal Excel value, and #VALUE! is the catch-all for "not a legal value". (There are other limitations to returned arrays besides jaggedness. For example, arrays over a certain size also result in a #VALUE! error.)
Note that nested arrays that are all the same length can be passed back from UDFs:
Public Function thisKindOfNestingWorks()
thisKindOfNestingWorks = Array(Array(1, 2), Array(3, 4))
End Function
This can be useful when you're building up some list-of-lists that you actually want coerced to a 2-D array.
So, calling your ARY function above like this should work just fine:
=ARY(ARY(A1, B1), ARY(C1, D1))
However, your TEST1 function would fail, since calling
=TEST1(ARY(ARY(A1, B1), ARY(C1, D1)))
would result in a 2-D array being passed to TEST1, not a 1-D array of 1-D arrays.
You might also find this question and my answer to it to be helpful:
Return a User Defined Data Type in an Excel Cell
MUCH LATER EDIT:
Incidentally, your 'ARY' function could be a lot shorter. This has nothing to do with your original question, but it's worth mentioning:
Public Function arr(ParamArray args())
arr = args
End Function
There is an example of using it in this answer:
Excel Select Case?

Related

vba: iterate through type matrix and write to worksheet

I need help concerning the handling of a type-matrix.
I want to create a matrix to perform certain operations in since that is much faster than doing the same in a normal worksheet. Besides I have several type matrices that operate on each other.
The part I struggle with is how I write the results to a worksheet. The only approach I have so far is to give each type in the matrix to a variant which I write to the worksheet. I guess there must be a more efficient way to write the "container_matrix" to a sheet other than to hardcode but I don't quite get it.
Is it to obvious on how to do this? Is there a way/handle to iterate through the items "container[1,2,3]" without naming them explicitly? Is the whole approach not suitable? I would like to keep the defined types "container1"
Public Type container_data
container1 As Double
container2 As Double
container3 As Double
End Type
Dim container_matrix() As container_data
ReDim container_matrix(5)
For int1 = 0 To 4
container_matrix(int1).container1 = 10
container_matrix(int1).container2 = 15
container_matrix(int1).container3 = 25
'**ignorance starts here:**
Dim var1 As Variant
ReDim var1(5, 3)
var1(int1, 0) = container_matrix(int1).container1
var1(int1, 1) = container_matrix(int1).container2
var1(int1, 2) = container_matrix(int1).container3
Next int1
Worksheets(1).Range("A1:C" & 5) = var1

How to read another cell from within a UDF?

I am aware that it's very hard to change the value of another cell using a User Defined Function - forbbiden by MS, even. However, I'd like to just read from another cell and do something with that information, but the function never runs thoroughly. For instance, in
Public Function ADD(arg1 as Double, arg2 as Double) as Double
If Worksheets("sheet1").Cells(1,1).Value = 0 Then
ADD = -1
Exit Function
End If
MsgBox "I got here"
ADD = arg1 + arg2
End Function
the message box does not show up. It can only be the if-statement. When I remove it, it works. Is there any way to read from another fixed cell without crashing the function? The real function I'm working with does all the reading from within if-statements.
Also, this can be found on the MS website, but I don't understand why it prevents me from reading..
For correct calculation, all ranges that are used in the calculation should be passed to the function as arguments. If you do not pass the calculation ranges as arguments, instead of referring to the ranges within the VBA code of the function, Excel cannot account for them within the calculation engine. Therefore, Excel may not adequately calculate the workbook to make sure that all precedents are calculated before calculating the user-defined function.
Thank you in advance!
I changed the UDF name to ADDD() and it works fine:
Public Function ADDD(arg1 As Double, arg2 As Double) As Double
If Worksheets("Sheet1").Cells(1, 1).Value = 0 Then
ADDD = -1
Exit Function
End If
MsgBox "I got here"
ADDD = arg1 + arg2
End Function
You cannot run a message box with a UDF. Can you image how many times you would have to click "OK" if you could and your sheet recalculated frequently?
Instead, you can use a debug.print statement, and you can run your UDF from the VBA IDE to check the debug values. For example:
Private Function TestMyUDF()
Dim examplevalue1 as Double
Dim examplevalue2 as Double
MyUDF examplevalue1, examplevalue2
End Function
Off-question, but if your referenced (read-only) cell is significant, then use a named range (and make it a workbook name, not a sheet one). Let us call it "CheckSumCell" for the sake of an example.
Public Function MyUDF(arg1 as Double, arg2 as Double) as Double
If ThisWorkbook.Names("CheckSumCell").Value = 0 Then '<-- assumes single cell
MyUDF = -1
Exit Function
End If
MyUDF= arg1 + arg2
End Function
This will then better survive cell relocation (insertion of rows or columns)! If you decide to use another cell, then just change the definition of the named range and you will not require to edit the macro/UDF at all.

Why declare function's type?

Sorry if this has been asked, but seriously can't find anything, so would also appreciate on how to search for this stuff.
So my question: what is the point of declaring the function's type in general? E.g. here 'as double'
Function myFunction(ByVal j As Integer) As Double
Return 3.87 * j
End Function
For a normal variable it has tons of benefits, like less memory, easier to see typos, but why here?
Edit: so, it's good because we can avoid errors, like it giving back a different type of values than expected.
Functions RETURN something. That type is the type of the return.
In your function:
Function myFunction(ByVal j As Integer) As Double
Return 3.87 * j
End Function
You are returning a decimal, so type Double make sense.
If you don't return anything, then you can declare it as a Sub.
And, for clarification, your function would throw a compile error. Unlike other languages, in VBA to return, we set the function name's value to the thing we want to return:
Function myFunction(ByVal j As Integer) As Double
myFunction=3.87 * j
End Function
Now we can call this function to get the Double value that it creates:
Sub testSub()
msgbox("This is the result of the function: " & myFunction(10))
End Sub
Which would launch a message box saying "This is the result of the function: 38.7"
Since I can't mark a comment the answer, let me quote:
#John Coleman
My opinion is that it a good thing to declare your return types because it increases the likelihood that the compiler will complain when you are doing something that really doesn't make sense.
Excel VBA is different from other programming languages in that it centers around a particular application: Excel.
Functions are useful in Excel VBA primarily because they can be typed directly into a cell on a sheet by an end user. User defined functions provide near infinite flexibility. The value the user defined function prints to Excel is formatted based on the function's type--and in a program which is about data visualization, formatting is a huge part.
For example, try putting these four functions into a blank worksheet module:
Function myInt(x, y) As Integer
myInt = x / y
End Function
Function myDouble(x, y) As Double
myDouble = x / y
End Function
Function myString(x, y) As String
myString = x / y
End Function
Function myVariant(x, y)
myVariant = x / y
End Function
Next, enter each of these functions into a different cell in the workbook. Use x=1 and y=2.
myInt produces "=0"
myDouble produces "=0.5"
myString produces "'0"
myVariant produces "=0.5"
If you're okay with Excel deciding how to format your result, that's your choice, but specifying the type offers an entire new level of control. For example, by simply declaring a function an integer, you can avoid having to devote a line of code to rounding. By declaring a function to be a string, you can avoid several lines of formatting code trying to get a number to be saved as text instead.

Redimensioning both dimensions of a multi-dimensional array in VBA (why a solution here won't work)

Firstly, I will point out that the question around redimensioning a multi-dimensional array has been discussed and answered here: Excel VBA - How to Redim a 2D array?.
My issue is that I am trying to apply this answer and it isn't quite going smoothly! The issue is with calling the Function. If I dimension the array before calling the function, Excel just tells me it can't assign to the array (presumably because I'm not telling it which element to assign to). If I don't dimension the array beforehand then the function falls apart when it is looking for the dimensions of the old array...because it doesn't have any, presumably.
I know I can do the below by reversing the way round the array builds and then transposing it but I have need further on to change both dimensions of the array so am trying to get it working here first.
I will admit I'm at 'losing the will to live' stage with this code as I have been battling it for weeks and am an amateur wannabe programmer, so I realise it might be a really simple answer but I can't see it at the moment. Any help gratefully received.
Here's my code (the Sub is called from another sub, where all other variables are defined)
Sub CalculateRank(row, coln, TempSums, TempProducts, Lead_count)
Dim Maj As Double
Dim CompareCount As Integer
Dim CompareArray(1, 1) '**I don't really want to dimension this array before the loop below.
Maj = WorksheetFunction.Round(Range("FJudges") / 2, 0)
For coln = 1 To Lead_count
CompareCount = 0
For row = 1 To Lead_count
If TempSums(row, coln) >= Maj Then
CompareCount = CompareCount + 1
CompareArray = ReDimPreserve(CompareArray, CompareCount, 3) '**This is the line that is calling the function (copied directly from the bottom of the page linked above) and giving the error
CompareArray(CompareCount, 1) = row
CompareArray(CompareCount, 2) = TempSums(row, coln)
CompareArray(CompareCount, 3) = TempProducts(row, coln)
End If
Next row
Next coln
End Sub
You do need to make it a 2D array before calling that function, but with a Redim statement instead of Dim. The problem is not in the function ReDimPreserve you're using, because it takes an input array and returns another one from scratch. Your problem is in the assignment statement:
CompareArray = ...
VBA does not permit assigning to a static array, which is the case because you declared it as:
Dim CompareArray(1, 1)
You need instead to declare it as a dynamic array, like this:
Dim CompareArray() ' <--- Optional declaration
ReDim CompareArray(0, 0) ' <--- First initialization should be with ReDim
p.s.
The declaration Dim CompareArray() is optional, but is considered by many as good practice. Basically you can omit it and declare directly using ReDim (even with Option Explicit). Just make sure no other variable with the same name exists in the same scope (which the compiler would alert about if the Dim statement is there).
I started with (0, 0) because this is the minimal size, instead of (1, 1) (EDIT: unless you are using Option Base 1 as it appeared in the comment).

How to implement an ISBETWEEN-like function for arrays of values without having to calculate the array more than once?

An ISBETWEEN function tests whether a value falls between a lower bound and a higher bound. With no native ISBETWEEN function in Excel, the value under test must be compared twice; first with '>' and then with '<' (or '>=' and '<=' for an ISBETWEEN test that is inclusive of the bounds.)
Comparing the value twice means having to calculate it twice, and this can be extremely expensive when that value is an array. With array functions being somewhat cryptic even at the best of times, doubling up on such a calculation also sends the readability of the function plummeting.
My question is whether anyone knows of a technique that delivers ISBETWEEN-like functionality for an array of values without the double calculation of that array? My preference is to do this with native Excel functionality but, if anyone has some great VBA, that would be good too.
Many thanks for your time!
Will
Building from my comment above: This doesn't provide a 100% answer to your question, but since it was pretty generic, I think this is the closest to an answer that I can get.
Imagine a spreadsheet set up like:
We can get a count of all the values that are between 3 and 5 using CTE/Array formula:
={SUM(IF(LOOKUP(A1:A6,{3,"B";6,"C"})="B",1,0))}
Results:
5
That's a pretty round-about way of doing this, but the array of A1:A6 only needs to be referenced once. Which is pretty cool.
Note that the squirrely brackets in the above formula aren't actually entered, but are placed by excel when you enter the array formula to indicate that it's an array formula... you probably already know that though if you've read this far.
So I've been able to develop a piece of VBA, based on the idea here.
Dim vValueArg As Variant, vLowerArg As Variant, vUpperArg As Variant, vTestLower As Variant, vTestUpper As Variant
Function ISBETWEEN(vValue As Variant, vLower As Variant, vUpper As Variant, Optional bInc As Boolean = True) As Variant
vValueArg = vValue
vLowerArg = vLower
vUpperArg = vUpper
If bInc Then
vTestLower = [GetValue() >= GetLower()]
vTestUpper = [GetValue() <= GetUpper()]
Else
vTestLower = [GetValue() > GetLower()]
vTestUpper = [GetValue() < GetUpper()]
End If
ISBETWEEN = [IF((GetTestLower() * GetTestUpper()) = 1, TRUE, FALSE)]
End Function
Function GetValue() As Variant
GetValue = vValueArg
End Function
Function GetLower() As Variant
GetLower = vLowerArg
End Function
Function GetUpper() As Variant
GetUpper = vUpperArg
End Function
Function GetTestLower() As Variant
GetTestLower = vTestLower
End Function
Function GetTestUpper() As Variant
GetTestUpper = vTestUpper
End Function
The first argument can be a single value, range or array. If a single value, then the next two arguments must also be single values (but this kinda defeats the purpose of the code!)
The second and third arguments can also be a single value, range or array. If a range consisting of multiple cells or array of multiple values, then the dimensions of these arguments must match those of the first argument. (NB - I have NOT tested the code with 2 dimensional ranges or arrays!)
The final, optional, argument determines whether the ISBETWEEN test is performed including or excluding the bounds. TRUE = include bounds; i.e. arg2 <= arg1 <= arg3 (the default, and can therefore be omitted). FALSE = exclude bounds; i.e. arg2 < arg1 < arg3.
While this might not be the prettiest code in the world, it is compact, fast (no loops) and copes with ranges and arrays of any size.
Hope some of you find this useful! :)

Resources