@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。
希望对您有所帮助。