【问题标题】:Compare two columns in excel: One to One Mapping比较excel中的两列:一对一映射
【发布时间】:2023-03-21 08:29:01
【问题描述】:

如果我在 excel 中有两列(单独的工作表):

Column 1  Column 2

A            B
B            E
C            F
C            G
F            C

我想确定第 1 列中的哪些项目存在于第 2 列中,反之亦然(即 B、C、F)。这可以通过条件格式或 VLOOKUP 来完成。

但是,第 1 列有 2 个 C。我想要一个精确的 1 对 1 匹配。所以,我希望第 1 列中的第一个 C 与第 2 列中的 C 匹配,但我不希望第 1 列中的第二个 C 与第 2 列中的相同 C 匹配。

我有什么方法可以使用宏(或其他任何东西)来识别一对一的匹配项?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    COUNTIF 有混合引用呢?

    类似这样的:

    在C2:=COUNTIF(B:B,A2)>=COUNTIF(A$2:A2,A2)

    在D2:=COUNTIF(A:A,B2)>=COUNTIF(B$2:B2,B2)

    这些可以很容易地做成条件格式规则:

    【讨论】:

      【解决方案2】:

      @BigBen 的答案是 Brillant。如果你想要一个匹配两个不同工作表中两列的宏,你可以试试这个代码:

      Sub test()
      
      Dim numRowsSheet1, numRowsSheet2 As Integer
      Dim ws1, ws2 As Worksheet
      
      Set ws1 = Worksheets("Sheet1")
      Set ws2 = Worksheets("Sheet2")
      
      
      With ws1
      'count how many rows are used on the A column sheet1
       numRowsSheet1 = .Range("A:A").Cells.SpecialCells(xlCellTypeConstants).count
      End With
      'MsgBox numRows
      
      With ws2
      'count how many rows are used on the A column sheet1
       numRowsSheet2 = .Range("A:A").Cells.SpecialCells(xlCellTypeConstants).count
      End With
      
      'macth column A sheet1 with sheet2
      Call myMatch(ws1, ws2, numRowsSheet1, numRowsSheet2)
      
      'macth column A sheet2 with sheet1
      Call myMatch(ws2, ws1, numRowsSheet2, numRowsSheet1) ' call my function
      
      End Sub
      
      'function where i do the match
      Function myMatch(ByVal ws As Worksheet, ByVal wss As Worksheet, ByVal numRows As Integer, ByVal numRowss As Integer)
      
      Dim myArray() As String
      Dim i, j As Integer
      
      ReDim myArray(numRows - 1) 'into this array i put the data used
      
      For i = 1 To numRows
      
          With ws
              
            myArray(i - 1) = .Cells(i, 1) 'put into array the items of the sheet
              
          End With
          
      Next i
      
      j = 0
      'macth items present into sheet
      Do
      For i = 1 To numRowss
      
          With wss
              
            If .Cells(i, 1) = myArray(j) Then
            
              .Cells(i, 2) = "true" ' next cell it writes true
              .Cells(i, 1).Interior.ColorIndex = 6 'yellow color
              j = j + 1
              If j > numRows - 1 Then
                  Exit For
              End If
            End If
              
          End With
          
      Next i
      j = j + 1
      Loop Until j > numRows - 1
      End Function
      

      结果是: 表 1

      进入 sheet2 是

      在意大利语中 VERO = TRUE。 希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 2017-05-27
        • 1970-01-01
        • 2018-04-08
        相关资源
        最近更新 更多