比较两列并突出显示文本(如果它包含第一列中的文本)

时间:2015-08-13 18:45:36

标签: excel comparison

我有两列

A                      |  B
--------------------------------------
123 main street        |  123
234 cello street       |  456
449 w 3rd street       |  585
1098 folsom blvd       |  1098
2323 new york street   |  1088
676 cherry street      |  676  

我的问题是,我们是否有机会比较A列和B列,并找出,如果B列的值在A列中。我们不是在比较整个文本,而只是比较A列的一部分B栏。

*注意:*我确定B列中两个单元格的组合不会出现在A中,例如,A34中不存在123456.

2 个答案:

答案 0 :(得分:1)

=IF(SEARCH(B1;A1;1);1;0)

如果文本存在,将为您提供1,如果不存在则为0。这就是你想要的吗?

答案 1 :(得分:1)

这可能不是最高性能的方式,但是有一个方便的VBA功能可以将范围转换为CSV,就在这里。 (按Alt + F11打开以访问VBA开发人员,并将其放入新模块中):

'**********************************************
'* PURPOSE: Concatenates range contents into a
'*          delimited text string
'*
'* FUNCTION SIGNATURE: Range2Csv(Range, String)
'*
'* PARAMETERS:
'*    Range  - the range of cells whose contents
'*             will be included in the CSV result
'*    String - delimiter used to separate values
'*             (Optional, defaults to a comma)
'*
'* AUTHOR: www.dullsharpness.com
'**********************************************
Public Function Range2Csv(inputRange As Range, Optional delimiter As String)
  Dim concattedList As String 'holder for the concatted CSVs
  Dim rangeCell As Range      'holder cell used in For-Each loop
  Dim rangeText As String     'holder for rangeCell's text

  'default to a comma delimiter if none is provided
  If delimiter = "" Then delimiter = ","

  concattedList = ""          'start with an empty string

  'Loop through each cell in the range to append valid contents
  For Each rangeCell In inputRange.Cells

    rangeText = rangeCell.Value 'capture the working value

    'Only operate on non-blank cells (i.e. Length > 0)
    If Len(rangeText) > 0 Then
      'Strip any delimiters contained w/in the value itself
      rangeText = WorksheetFunction.Substitute(rangeText, delimiter, "")

      If (Len(concattedList) > 0) Then
        'prepend a delimiter to the new value if we
        'already have some list items
        concattedList = concattedList + delimiter + rangeText
      Else
        'else if the list is blank so far,
        'just set the first value
        concattedList = rangeText
      End If
    End If

  Next rangeCell

  'Set the return value
  Range2Csv = concattedList

End Function

假设您要搜索的范围是A1:A20,您可以突出显示搜索字词单元格(在此示例中以单元格B1开头)并输入此条件格式设置公式:

=ISNUMBER(FIND(B1,range2csv($A$1:$A$20,";")))

这将首先生成所有搜索目标的字符串(它将每个单元格连接成一个连续的字符串),然后逐个检查连接字符串中是否存在搜索项(B列条目)。 / p>

如上所述,它可能不是最高效的方式,但这是一个快速的解决方案。

Range2Csv Function Writeup