您可以使用字典來做到這一點。使用字典意味着您只讀取sheet1中的值,而不是讀取sheet2中的每個值。
Sub CompareColumns()
Dim dict As Object
Set dict = CreateObject("Scripting.dictionary")
Dim sheet1 As Worksheet, Sheet2 As Worksheet
Set sheet1 = ThisWorkbook.Worksheets("Sheet1")
Set Sheet2 = ThisWorkbook.Worksheets("Sheet2")
' Read values from sheet1 to dictionary
Dim lastRow As Long
lastRow = sheet1.Cells(sheet1.Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = 1 To lastRow
' Store value to dictionary
dict(sheet1.Cells(i, 1).Value) = 1
Next
' Read from sheet2 and check if each value exists
lastRow = Sheet2.Cells(Sheet2.Rows.Count, 2).End(xlUp).Row
For i = 1 To lastRow
' Check if value exists in dictionary
If dict.exists(Sheet2.Cells(i, 2).Value) Then
' found
Else
' not found
End If
Next
End Sub