VBA Searching Value in Array

maria90

New Member
Joined
Apr 9, 2012
Messages
38
I have an array with orders (order date, customer etc) and I need to check whether a specific ID corresponds to a customer.
Here is the function / data below:

Code:
Function CheckCustomer(ID As Integer, Customer As Integer) as Boolean


Dim arr() As Variant
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Orders")
arr = ws.Range("tblOrders")
Dim i As Long
    
    'Check if the customer does have such a contract
    For i = 1 To UBound(arr)
        If arr(i, 2) = Customer And arr(i, 1) = ID Then
                    CheckCustomer = True
                Else
                    CheckCustomer = False
                End If
    Next i


End Function

If I use CheckCustomer(108,723) I get FALSE - even though the customer is clearly in the table with the correct ID.

As I am working with array data, I was wondering whether the
return 'true' value is overwritten as soon as a 'false' is found in the array.

Is there any other way to search for these value, like with SUMPRODUCT?

Thanks for your help.

Maria



IDCustomer
108723
159723
490723
292723
381723

<tbody>
</tbody>
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try exiting the function once a match is found...

Code:
    [COLOR=green]'Check if the customer does have such a contract[/COLOR]
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](arr)
        [COLOR=darkblue]If[/COLOR] arr(i, 2) = Customer And arr(i, 1) = ID [COLOR=darkblue]Then[/COLOR]
            CheckCustomer = [COLOR=darkblue]True[/COLOR]
            [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Function[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] i
    
    CheckCustomer = [COLOR=darkblue]False[/COLOR]

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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