Problem with array formula and user defined function

Hercules1946

Well-known Member
Joined
Oct 6, 2007
Messages
545
I have an array formula thats pulling the lines from sheet A to sheet B based on a criteria column in A If the criteria says "Cash" or "Cheque" that data is pulled onto B.
Excel builds two arrays one showing the contents of the A criteria column and the other a set of TRUE and FALSE indicating (correctly) whether the criteria is met.
SMALL then works with the row numbers to find the lowest row number for the TRUE criteria routed by IF(TRUE)

(a) Both the row numbers array and the (TRUE/FALSE) array (30 elements in each) are correctly listed and lined up with each other by Evaluate Formula. Now I can state my problem.

If Im using FIND( to do the criteria test, then the correct row is returned. If I use my UDF it isn't. Everything is the same at point (a) with both functions.
The FIND route matches the two arrays on a one for one basis giving me 30 elements returning the line numbers (TRUE) and returning FALSE (FOR FALSE). (SMALL returns the correct row).
Th UDF route matches all the TRUE/FALSE with every entry in the row numbers so that SMALL picks 1 which is always available along with all the others

For info, my UDF uses Instr to check that the tested value is in the correct section of the (lookup) string of values (eg having a startpoint at position before <=5, as in "CashCheque....." ).

Why are the arrays handled differently by the two functions ? How can I get the UDF to work like FIND? Ive tried everything I can think of on this with no luck.

Any help would be appreciated.

This is also posted at:
Problem with Look Up Array Formula


will advise when resolved
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Please post your UDF.

Hello Andrew
Thanks for your response.
Here is the coding for the UDF:
Code:
Public Function Fstr(MyRange As Range) As Variant
Dim TestString As String, Mystring As String
TestString = "CashChequeEFTPOS"
Dim i As Long, rng As Variant, Arr() As Variant
i = MyRange.Cells.Count
ReDim Arr(1 To i)
i = 1
For Each rng In MyRange
    Arr(i) = InStr(TestString, rng.Value)
    i = i + 1
Next rng
Fstr = Arr

End Function
 
Upvote 0
What is the array formula that fails?
It looks to me that your UDF will return a row-wise array.
Might using TRANSPOSE(Fstr(someRange)) fix the issue?

Or perhaps this, which returns an array the same shape as myRange.

Code:
Function Fstr(MyRange as Range) As Variant
Dim TestString As String, Mystring As String
TestString = "CashChequeEFTPOS"
Dim i As Long, j As Long, rng As Variant, Arr As Variant

If myRange.Cells.Count = 1 Then
    Redim Arr(1 to 1, 1 to 1)
    Arr(1, 1) = InStr(TestString, myRange.Value)
Else
    Arr = myRange.Value:Rem equivalent of ReDim

    For i = 1 to myRange.Rows.Count
        For j = 1 to myRange.Columns.Count
            Arr(i,j) = InStr(TestString, myRange.Cells(i, j).Value)
        Next j
    Next i
End If
 
Fstr = Arr

End Function
 
Last edited:
Upvote 0
Hello Andrew/Mike
Here are some shots from my spreadsheet. Basically I started off using FIND which seemed to work OK as shown in B6 of the Extract Sheet (The second shot). The B cells should only return "Cash" or "Cheque" from the Source sheet (the first shot).
As a trial, I used the UDF Fstr in B5 and that returns EFTPOS which is wrong. Dont pay any attention to cols C to E as they may confuse. Col B is the problem

Source Table with the Named Ranges


Excel 2010
BCDE
3Payment MethodDateReceipt #Name
4
5EFTPOS30/10/13230Fred Smith
6Cheque30/10/13231John Doe
7EFTPOS30/10/13232Luke Harris
8Cash31/10/13233Darren Loser
CASH BOOK


Extract Table:


Excel 2010
BCDE
3Payment MethodDateReceipt #Name
4
5EFTPOS30/10/13231John Doe
6Cash31/10/13233Darren Loser
7Cheque1/11/13234Brad Walks
8Cash2/11/13236Jane Doe
9Cheque2/11/13237Hanry Whinst
10Cheque2/11/13238Fred Brown
Cash Book - Cash & Chq
Cell Formulas
RangeFormula
B5{=IFERROR(INDEX(METH,SMALL(IF(FSTR(METH) <= 5, ROW(METH)-4),ROWS(B$4:B4))),"")}
B6{=IFERROR(INDEX(METH,SMALL(IF(FIND(METH,PM,1) <= 5, ROW(METH)-4),ROWS(B$4:B5))),"")}
Press CTRL+SHIFT+ENTER to enter array formulas.
Named Ranges
NameRefers ToCells
METH='CASH BOOK'!$B$5:$B$34
PM=Sheet1!$D$3
 
Last edited:
Upvote 0
Sorry - ran out of edit time.

The Range PM = "CashChequeEFTPOS" (Sheet1!D3)

If you need anything else, let me know

Thanks
Hercules :)
 
Upvote 0
Did you try my variation of the UDF?
TRANSPOSE (either in the UDF or in the formula) shifts the problem to the case where you use Fstr on a A1:Z1.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top