查找定义的表中是否存在所有单元格值(用逗号分隔)

时间:2019-05-07 17:01:59

标签: excel vba excel-formula

这是我的报告样本:

pic1

基本上,该报告包含大量的供应商,在这些供应商中,我需要确定其中哪些供应商具有同一国家/地区的所有实体(内容组),而忽略“集成”标签。每个国家/地区的实体分别在表格中定义(右)。

到目前为止,我尝试将= SUMPRODUCT(-(ISNUMBER(SEARCH()))组合使用,但是总是部分得到我想要的东西。

在C列中,需要:

  • 如果该行的供应商具有所提及国家代码的所有实体,则显示YES;
  • 否则显示否;

我对此的逻辑:

该公式需要从第一个表中选择国家/地区代码,然后查看定义了实体的第二个表,并检查内容组中的所有实体是否匹配,而忽略“ integrate”(集成),这是应用的默认标记到处都是

预期结果:

Pic2

2 个答案:

答案 0 :(得分:1)

如果您的Excel 2013+版本具有$object.property = value函数,则可以使用以下数组公式:

.Save()
  • 我们删除了FILTERXML
  • 从表1中的字符串创建一个=IF(OR(ISNA(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"Integrate",""),", ",","),",","</s><s>")&"</s></t>","//s"),INDIRECT("Table2["&B2&"]"),0))),"No","Yes")
  • 提取Integrate
  • 的每个元素
  • 尝试在表2的相应列中找到它们
  • 如果找不到一个,则说明它有多个国家/地区。

由于这是一个数组公式,因此您需要在按下 enter 的同时按住 ctrl + shift 来“确认”它。如果操作正确,Excel将按照公式栏中的说明在公式周围放置括号XML

enter image description here

如果您具有不具有此功能的Excel版本,并且您仍然对使用Excel公式而不是VBA感兴趣,那么我们可以使用另一个公式。

答案 1 :(得分:0)

尝试:

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