如果我在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列中的第一个C与第2列中的C相匹配,但我不希望第1列中的第二个C与第2列中的相同C相匹配。
我可以使用宏(或其他任何方式)识别1对1匹配吗?
答案 0 :(得分:1)
带有混合引用的COUNTIF
怎么样?
类似这样的东西:
在C2
中:=COUNTIF(B:B,A2)>=COUNTIF(A$2:A2,A2)
在D2
中:=COUNTIF(A:A,B2)>=COUNTIF(B$2:B2,B2)
可以很容易地将它们设置为条件格式规则:
答案 1 :(得分:0)
@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
用意大利语VERO = TRUE。 希望对您有帮助。