Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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_Countif - Fatal编程技术网

Excel 确定指定范围内是否有匹配值

Excel 确定指定范围内是否有匹配值,excel,vba,countif,Excel,Vba,Countif,我有200个数据输入行。我需要知道当前单元格值(示例B6)是否与列B(示例B1:B4)中的任何上述值匹配。如果是,请将“x”放在d列的当前行中 当前公式=IF(COUNTIF($B$5:B35,B36)>0,“x”,“”)我不确定您是否需要有关公式本身或编写执行相同功能的等效VBA函数的帮助。因此,我有两种选择: 使用Excel的COUNTIF 假设您的数据从单元格A1开始,并有一个标题行。那么,你必须 。。。在单元格D2中放置一个与已有公式类似的公式,=IF(COUNTIF(B$2:B2,B

我有200个数据输入行。我需要知道当前单元格值(示例B6)是否与列B(示例B1:B4)中的任何上述值匹配。如果是,请将“x”放在d列的当前行中


当前公式
=IF(COUNTIF($B$5:B35,B36)>0,“x”,“”)

我不确定您是否需要有关公式本身或编写执行相同功能的等效VBA函数的帮助。因此,我有两种选择:

使用Excel的COUNTIF

假设您的数据从单元格A1开始,并有一个标题行。那么,你必须

  • 。。。在单元格D2中放置一个与已有公式类似的公式,
    =IF(COUNTIF(B$2:B2,B2)>1,“x”,“”)
  • 。。。将D2中的公式复制到D列的单元格中,直到到达最后一行数据
例如,在第36行中使用“B$2:B36”和“>1”的原因(与“B$2:B35”和“>0”的方式相反)是,它允许您在标题行之后的所有行中使用公式。否则,您必须将D2保留为没有公式,并将第一个公式添加到D3中

“B$2:B2”中使用“$”的原因是,它强制公式始终从表的顶部进行搜索,无论将公式复制到哪一行

公式如下所示:


使用VBA

如果要使用VBA函数而不是Excel的
COUNTIF
(希望获得更快的结果),可以创建如下函数:

Function ValueRepeats(ByVal valuesRange As Range)
  Const headerRow = 1 'If there is no header, you can use 0 instead of 1

  'Assume that the value will repeat itself
  ValueRepeats = True

  Dim values As Variant: values = valuesRange.Value

  'If [values] is not an array, it is a single value, meaning that it cannot have a duplicate
  If Not IsArray(values) Then
    ValueRepeats = False
    Exit Function
  End If

  'Get the 2-dimensional array's bounds
  Dim arrLb As Long: arrLb = LBound(values, 1)
  Dim arrUb As Long: arrUb = UBound(values, 1)
  Dim index2 As Long: index2 = LBound(values, 2) 'In the 2nd dimension, we only
                                                 '  care about the first column

  'Get the value to search for (the last value in the array)
  Dim lastValue As Variant: lastValue = values(arrUb, index2)

  'Traverse the array and compare the elements against the last value
  Dim i As Long
  For i = arrLb To arrUb - 1
    If ValuesMatch(lastValue, values(i, index2)) Then Exit Function
  Next

  ValueRepeats = False 'If we are here, no repeat value was found
End Function

Private Function ValuesMatch(ByVal v1 As Variant, ByVal v2 As Variant)
  'NOTE: This function treats the string "5" and the number 5 as different values;
  '  also, string comparisons are case-insensitive,
  '  and Null, Empty, and "" are considered equivalent; all of this can be changed as needed

  Dim typ1 As Integer: typ1 = VarType(v1)

  'Make sure the values are of the same type (to avoid confusing numbers and dates),
  '  unless the values can be converted to an empty string
  If typ1 <> VarType(v2) Then
    ValuesMatch = (v1 & "") = (v2 & "") 'Null, Empty, and "" will match each other
    Exit Function
  End If

  Select Case typ1
    Case vbNull
      ValuesMatch = True 'v1=v2 does not work if both values are null
    Case vbString
      ValuesMatch = StrComp(v1, v2, vbTextCompare) 'Case-insensitive string comparison
    Case Else
      ValuesMatch = v1 = v2
  End Select
End Function
然后,您必须将此公式复制到D列的其余部分。公式如下所示:

为了更好地理解索引和匹配,我建议使用如下页面:



你的问题是什么?您描述了一个场景,并发布了用于计算该场景的公式。还要注意,VB和VBA是不同的。决定你想用什么。如果您正在编写代码,请发布您的代码并解释您被卡住的地方。欢迎!如果您将此问题编辑为包括以下内容,将会很有帮助:1。您正在处理的一些示例数据,包括列标题;2.一个你希望看到的例子;三,。你真正得到的。
=IF(ISNA(INDEX(B$2:B2, MATCH(B3, B$2:B2, 0),1)), "", "x")