简体   繁体   中英

Finding if a cell values (delimited by comma) are all existing in a defined table

Here is a sample of the report I have:

PIC1

Basically the report consists in a huge list of suppliers where among other things, I need to identify which of them have all entities (content groups) for the same country, while ignoring the "integrate" tag. Entities for each country are defined in a table separately (right).

So far I tried a combination of =SUMPRODUCT(--(ISNUMBER(SEARCH())) but always getting partially what I want.

In column C, in need:

  • to display YES if the supplier on that row has all entities for the mentioned country code;
  • to display NO otherwise;

My logic on this:

The formula/s needs to pick the country code from 1st table, then look into the 2nd table where entities are defined and check if all the entities in the content group are matching, ignoring "integrate" which is a default tag applied everywhere.

Expected result:

PIC2

If you have a version of Excel 2013+ which has the FILTERXML function, you can use this array formula:

=IF(OR(ISNA(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"Integrate",""),", ",","),",","</s><s>")&"</s></t>","//s"),INDIRECT("Table2["&B2&"]"),0))),"No","Yes")
  • We remove the Integrate
  • Create an XML from the strings in Table1
  • Extract each element of the XML
  • Try to find them in the appropriate column of Table2
  • If we don't find one, then it has multiple countries.

Since this is an array formula, you need to "confirm" it by holding down ctrl + shift while hitting enter . If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar

在此处输入图片说明

If you have a version of Excel that does not have this function, and you are still interested in using excel formulas as opposed to VBA, there is another formula we can use.

Try:

Option Explicit

Sub test()

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim LastRowA As Long, i As Long, y As Long
    Dim arr As Variant
    Dim CountryCode As String
    Dim rng As Range, SearchRange As Range, FindPosition As Range
    Dim Appears As Boolean

    'Set worksheets on variables
    With ThisWorkbook
        Set ws1 = .Worksheets("Sheet1")
        Set ws2 = .Worksheets("Sheet2")
    End With

    'Set the range to search in for country codes
    Set SearchRange = ws2.Range("H1:R1")

    With ws1

        'Find the last row of Column A sheet1
        LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Start loop from row 2 to last row sheet1
        For i = 2 To LastRowA

            'Criteria needed ( Column A - Not empty cell, Column D - Includes "Europe" & Column E - Includes "No" Columns D and E are CASE SENSITIVE)
            If .Range("A" & i).Value <> "" And .Range("D" & i).Value = "Europe" And .Range("E" & i).Value = "No" Then

                CountryCode = .Range("B" & i).Value

                'In which column the country code found
                Set FindPosition = SearchRange.Find(What:=CountryCode, LookIn:=xlValues, LookAt:=xlWhole)

                'If code excist
                If Not FindPosition Is Nothing Then

                    'Set the range to search for the groups in the column where the code is header
                    Set rng = ws2.Range(ws2.Cells(2, FindPosition.Column), ws2.Cells(ws2.Cells(ws2.Rows.Count, FindPosition.Column).End(xlUp).Row, FindPosition.Column))

                    'Split the string with comma and assing it on arr
                    arr = Split(.Range("A" & i).Value)

                    Appears = False

                    'Loop the arr
                    For y = LBound(arr) To UBound(arr)

                        'Check if the arr(y) start from C as all code start from C
                        If Left(arr(y), 1) = "C" Then

                            'Count how many times the arr(y) with out the comma appears in the rng
                            If Application.WorksheetFunction.CountIf(rng, Replace(arr(y), ",", "")) > 0 Then
                                'If appears the variable Appears is true
                                Appears = True
                            Else
                                'If does not appear the variable Appears is False & Exit the loop
                                Appears = False
                                Exit For
                            End If

                        End If

                    Next y

                    'Check Appears variable status and import value in Column C
                    If Appears = True Then
                        .Range("C" & i).Value = "Yes"
                    Else
                        .Range("C" & i).Value = "No"
                    End If

                'If code does not excist
                Else: MsgBox "Country Code not does not excist."

                End If

            End If

        Next i

    End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM