繁体   English   中英

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

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

这是我的报告样本:

PIC1

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

到目前为止,我尝试了= SUMPRODUCT(-(ISNUMBER(SEARCH()))的组合,但始终得到部分想要的东西。

在C列中,需要:

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

我对此的逻辑:

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

预期结果:

PIC2

如果您具有具有FILTERXML函数的Excel 2013+版本,则可以使用以下数组公式:

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

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

在此处输入图片说明

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

尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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