Use Ranged Match or Countif on dual columns

HansSchulze

New Member
Joined
Jul 27, 2017
Messages
4
The Countif below should read something like
Code:
...partNumber & companyName, Range("MPN!PDPMPN[MFR_PART_NUMBER] & Range("MPN!PDPMPN[ORGANIZATION_NAME")
to concatenate the two fields for the search. Unfortunately, MPN table columns are A and D are not consecutive.

Code:
    Dim company As Variant, rDataMart As Range, rDataMart2, where As Variant
    Set rDataMart = ThisWorkbook.Worksheets("MPN").Range("MPN!PDPMPN[MFR_PART_NUMBER]")
    Set rDataMart2 = ThisWorkbook.Worksheets("MPN").Range("MPN!PDPMPN[ORGANIZATION_NAME]")
    GetNearestCompany = ""
    For Each company In NameArray
        where = Application.CountIfs(rDataMart, MPN, rDataMart2, company) ' still not working
        If where Then
            If GetNearestCompany = Empty Then
                Debug.Print "Found", MPN, company, where
                GetNearestCompany = where
            Else
                Debug.Print "Dup", MPN, company, GetNearestCompany, where
            End If
        End If
    Next company
    If GetNearestCompany = Empty Then
        Debug.Print "NF", MPN, company
    End If

Thanks for your help in advance.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
It may be a datatypes issue. Latest try is from another post's suggestion using Match and Index, but it seems that Index is originally recognized by mouseover help, but goes "index" after I finish, and claims Type Mismatch error.

Code:
 Function GetNearestCompany(MPN As Variant, MySupplier As Variant) As Variant
dim where as Variant
...
where = Application.WorksheetFunction.Match(1, Application.WorksheetFunction.index((rDataMart = MPN) * (rDataMart2 = company), 0), 0)
which resembles this code for Excel: =MATCH(1,INDEX(($A$1:$A$6="id2")*($B$1:$B$6="day1"),),0)

What's the right syntax?
 
Upvote 0
Works with lots of digging on this and other excel forums.
Basically each search (A=RANGEA) returns an array of true/false, which can be multiplied by (B=RANGEB), gives an array of zeros, and just a few ones where the data matches, then Match looks for the first 1.

Code:
    Dim company As Variant, rDataMart As Range, rDataMart2, where As Variant, what As Variant
    Set rDataMart = ThisWorkbook.Worksheets("MPN").Range("MPN!PDPMPN[MFR_PART_NUMBER]")
    Set rDataMart2 = ThisWorkbook.Worksheets("MPN").Range("MPN!PDPMPN[ORGANIZATION_NAME]")
    GetNearestCompany = ""
    For Each company In NameArray
        what = "=Match(1,--(""" & MPN & """=" & rDataMart.Address & ") * --(""" & company & """=" & rDataMart2.Address & "), false)"
        where = Evaluate(what)
        If Not IsError(where) Then
            If GetNearestCompany = Empty Then
                Debug.Print "Found", MPN, company, where
                GetNearestCompany = where
            Else
                Debug.Print "Dup", MPN, company, GetNearestCompany, where
            End If
        End If
    Next company
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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