Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I can't access array elements

Status
Not open for further replies.

beakerboy

Technical User
May 31, 2001
27
US
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:
Code:
     A    B
1    1    1
2    ""   2
3    2    3
4    ""   5
5    3    #NA or ""
6    ""   #NA or ""
7    5    #NA or ""
and the function is:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top