比较两个范围:经典范围与单个单元格中逗号分隔值的范围
Compare two ranges: classic range vs range of comma-delimited values in a single cell
假设我有一系列值:
6 2 4 7 8 7
1 5 1 9 4 6
8 8 1 7 4 2
并分别在一个单元格中:
“1、4、9”
我需要将单个单元格中的这些值与给定范围进行比较。如果匹配 - 输入 "Yes",如果不匹配 - "No".
"1, 4, 9" - 结果:"Yes"
"3, 10, 11" - 结果:"No"
这里我有一个 UDF,它简单地比较两个普通范围并输入值匹配的次数作为结果。我想应该可以作为一种模板。
Public Function Compare(r1 As Range, r2 As Range) As Long
Dim r As Range, v As Variant, v2 As Variant
Dim rr As Range
For Each r In r1
v = r.Value
If v <> 0 And v <> "" Then
For Each rr In r2
v2 = rr.Value
If v = v2 Then Compare = Compare + 1
Next rr
End If
Next r
End Function
我修改了代码以拆分第一个单元格区域:
Public Function Compare(r1 As Range, r2 As Range) As Long
Dim r As Integer, v As Variant, v2 As Variant
Dim rr1() As String
Dim rr As Range
rr1 = Split(r1, ",")
For r = LBound(rr1) To UBound(rr1)
v = --Trim(rr1(r))
If v <> 0 And v <> "" Then
For Each rr In r2
v2 = rr.Value
If v = v2 Then Compare = Compare + 1
Next rr
End If
Next r
End Function
要获得 "Yes/No",您可以这样称呼它:
=IF(Compare(A1,$D:$D)>0,"Yes","No")
假设我有一系列值: 6 2 4 7 8 7 1 5 1 9 4 6 8 8 1 7 4 2
并分别在一个单元格中: “1、4、9”
我需要将单个单元格中的这些值与给定范围进行比较。如果匹配 - 输入 "Yes",如果不匹配 - "No".
"1, 4, 9" - 结果:"Yes"
"3, 10, 11" - 结果:"No"
这里我有一个 UDF,它简单地比较两个普通范围并输入值匹配的次数作为结果。我想应该可以作为一种模板。
Public Function Compare(r1 As Range, r2 As Range) As Long
Dim r As Range, v As Variant, v2 As Variant
Dim rr As Range
For Each r In r1
v = r.Value
If v <> 0 And v <> "" Then
For Each rr In r2
v2 = rr.Value
If v = v2 Then Compare = Compare + 1
Next rr
End If
Next r
End Function
我修改了代码以拆分第一个单元格区域:
Public Function Compare(r1 As Range, r2 As Range) As Long
Dim r As Integer, v As Variant, v2 As Variant
Dim rr1() As String
Dim rr As Range
rr1 = Split(r1, ",")
For r = LBound(rr1) To UBound(rr1)
v = --Trim(rr1(r))
If v <> 0 And v <> "" Then
For Each rr In r2
v2 = rr.Value
If v = v2 Then Compare = Compare + 1
Next rr
End If
Next r
End Function
要获得 "Yes/No",您可以这样称呼它:
=IF(Compare(A1,$D:$D)>0,"Yes","No")