Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 查找定义的表中是否存在单元格值(以逗号分隔)_Excel_Vba_Excel Formula - Fatal编程技术网

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

Excel 查找定义的表中是否存在单元格值(以逗号分隔),excel,vba,excel-formula,Excel,Vba,Excel Formula,以下是我的报告样本: 基本上,该报告包含一个庞大的供应商列表,其中除其他事项外,我需要确定哪些供应商拥有同一国家/地区的所有实体(内容组),同时忽略“集成”标签。每个国家的实体在表中分别定义(右) 到目前为止,我尝试了=SUMPRODUCT(-(ISNUMBER(SEARCH())的组合,但总是部分得到我想要的 在C栏中,需要: 如果该行的供应商拥有所述国家代码的所有实体,则显示是 除此之外不显示任何内容 我的逻辑是: 公式需要从第一个表中选择国家/地区代码,然后查看定义实体的第二个表,并

以下是我的报告样本:

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

到目前为止,我尝试了=SUMPRODUCT(-(ISNUMBER(SEARCH())的组合,但总是部分得到我想要的

在C栏中,需要:

  • 如果该行的供应商拥有所述国家代码的所有实体,则显示是
  • 除此之外不显示任何内容
我的逻辑是:

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

预期结果:

试试:

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
选项显式
子测试()
将ws1标注为工作表,将ws2标注为工作表
我和你一样长,我和你一样长
作为变体的Dim-arr
将国家代码设置为字符串
Dim rng作为范围,SearchRange作为范围,FindPosition作为范围
Dim显示为布尔值
'在变量上设置工作表
使用此工作簿
设置ws1=.Worksheets(“Sheet1”)
设置ws2=.Worksheets(“Sheet2”)
以
'设置搜索国家/地区代码的范围
设置SearchRange=ws2.Range(“H1:R1”)
使用ws1
'查找A列sheet1的最后一行
LastRowA=.Cells(.Rows.Count,“A”).End(xlUp).Row
'从第2行开始循环到最后一行sheet1
对于i=2至LastRowA
'所需标准(A列-非空单元格,D列-包括“Europe”和E列-包括“No”D和E列区分大小写)
如果.Range(“A”&i).Value”和.Range(“D”&i).Value=“欧洲”和.Range(“E”&i).Value=“否”,则
CountryCode=.Range(“B”&i).Value
'在哪个列中可以找到国家代码
设置FindPosition=SearchRange.Find(What:=CountryCode,LookIn:=xlValues,LookAt:=xlWhole)
'如果代码是激子
如果找不到,那么位置什么都不是
'设置范围以在代码为标题的列中搜索组
Set rng=ws2.Range(ws2.Cells(2,FindPosition.Column),ws2.Cells(ws2.Cells(ws2.Rows.Count,FindPosition.Column),End(xlUp.Row,FindPosition.Column))
'用逗号拆分字符串并在arr上赋值
arr=Split(.Range(“A”&i).Value)
出现=错误
'循环arr
y=LBound(arr)至UBound(arr)
'检查arr(y)是否从C开始,因为所有代码都从C开始
如果左(arr(y),1)=“C”,则
'计算不带逗号的arr(y)在rng中出现的次数
如果Application.WorksheetFunction.CountIf(rng,Replace(arr(y),“,”,”)>0,则
'如果出现,则变量显示为true
出现=真
其他的
'如果未显示,则变量显示为False&退出循环
出现=错误
退出
如果结束
如果结束
下一个y
'检查将在C列中显示变量状态和导入值
如果出现=True,则
.Range(“C”和i)。Value=“是”
其他的
.Range(“C”和i)。Value=“否”
如果结束
'如果代码未激活
其他:MsgBox“国家/地区代码未列出。”
如果结束
如果结束
接下来我
以
端接头
试试:

选项显式
子测试()
将ws1标注为工作表,将ws2标注为工作表
我和你一样长,我和你一样长
作为变体的Dim-arr
将国家代码设置为字符串
Dim rng作为范围,SearchRange作为范围,FindPosition作为范围
Dim显示为布尔值
'在变量上设置工作表
使用此工作簿
设置ws1=.Worksheets(“Sheet1”)
设置ws2=.Worksheets(“Sheet2”)
以
'设置搜索国家/地区代码的范围
设置SearchRange=ws2.Range(“H1:R1”)
使用ws1
'查找A列sheet1的最后一行
LastRowA=.Cells(.Rows.Count,“A”).End(xlUp).Row
'从第2行开始循环到最后一行sheet1
对于i=2至LastRowA
'所需标准(A列-非空单元格,D列-包括“Europe”和E列-包括“No”D和E列区分大小写)
如果.Range(“A”&i).Value”和.Range(“D”&i).Value=“欧洲”和.Range(“E”&i).Value=“否”,则
CountryCode=.Range(“B”&i).Value
'在哪个列中可以找到国家代码
设置FindPosition=SearchRange.Find(What:=CountryCode,LookIn:=xlValues,LookAt:=xlWhole)
'如果代码是激子
如果找不到,那么位置什么都不是
'设置范围以在代码为标题的列中搜索组
Set rng=ws2.Range(ws2.Cells(2,FindPosition.Column),ws2.Cells(ws2.Cells(ws2.Rows.Count,FindPosition.Column),End(xlUp.Row,FindPosition.Column))
'用逗号拆分字符串并在arr上赋值
arr=Split(.Range(“A”&i).Value)
出现=错误
'循环arr
y=LBound(arr)至UBound(arr)
'检查arr(y)是否从C开始,因为所有代码都从C开始
如果左侧(arr(y),1)=
=IF(OR(ISNA(MATCH(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"Integrate",""),", ",","),",","</s><s>")&"</s></t>","//s"),INDIRECT("Table2["&B2&"]"),0))),"No","Yes")