I'm trying to move some matlab code over to vba. One commonly used matlab function takes a vector of booleans and extracts only those elements from another vector where the boolean value at the same position is true.
My problem is I keep getting #VALUE errors if I try to access the myBool array outside the return statement.
I'm calling the function as '=newFunc(A1:A7<>"", A1:A7)'
Where the excel spreadsheet is(input is in A and the output I'm wanting is in B:
and the function is:
Thanks in advance,
Kevin Nowaczyk
My problem is I keep getting #VALUE errors if I try to access the myBool array outside the return statement.
I'm calling the function as '=newFunc(A1:A7<>"", A1:A7)'
Where the excel spreadsheet is(input is in A and the output I'm wanting is in B:
Code:
A B
1 1 1
2 "" 2
3 2 3
4 "" 5
5 3 #NA or ""
6 "" #NA or ""
7 5 #NA or ""
Code:
Public Function NewFunc(myBool As Variant, myRange As Range)
Dim myResults()
ReDim myResults(Application.Caller.Rows.Count)
number_of_results = 0
For i = 0 To Application.Caller.Rows.Count
'If myBool(i + 1, 1) Then 'Everything works as long as this line is commented out
myResults(number_of_results) = myRange.Rows(i + 1).Value
'MyResults(i) = myBool(i+1,1) 'I can't access myBool here either
number_of_results = number_of_results + 1
'End If
Next i
NewFunc = Application.WorksheetFunction.Transpose(myResults)
'NewFunc = myBool 'This Works if the above remain commented out
'NewFunc = myBool(2,1) 'This Works if the above remain commented out
End Function
Thanks in advance,
Kevin Nowaczyk