我試圖在一個工作表中將2列與另一個工作表中的2列(或可能更多)列進行匹配。如何檢查使用VBA比較值的兩個不同工作表上的列表
我已經發布了一份我的工作表數據樣本,希望對過去一週我一直在嘗試做的事情給予很好的描述。我認爲我處於正確的軌道上,但我不知道如何在兩張工作表之間進行適當的引用。
我想什麼做的是看位置1列,然後看是否符合我的RefSheet該位置引用任一位置1或位置2
如果是這樣,我想看看位置2則在RefSheet上的相同行中的某處匹配。如果匹配,我想突出顯示單元格/單元格黃色,並從RefSheet中提供ID編號。
如果沒有匹配,我想要突出顯示爲紅色或不突出顯示。
Sheet All
A B C D
ID Location 1 Location 2 Given ID
1 West North
2 North South
3 South East
4 East West
5 East East
6 South West
Sheet RefSheet
A B C
ID Location 1 Location 2
1 West North
2 West East
3 South East
4 South North
What it should look like on the original Worksheet
A B C D
ID Location 1 Location 2 Given ID
1(Yellow) West North 1
2(Yellow) North South 4
3(Yellow) South East 3
4(Yellow) East West 2
5(Red) East East
6(Red) South West
這是我可怕的代碼
Sub roadfinder()
Dim lngLast As Long
Dim lngCounter As Long
Dim rCell As Range
Dim lCnt As Long
Dim nextIntersection
Dim RefSheet As Worksheet
Dim list As Worksheet
Set intersections = ThisWorkbook.Sheets("RefSheet")
Set crashes = ThisWorkbook.Sheets("All")
Application.ScreenUpdating = False
lngLast = Cells(Rows.Count, "B").End(xlUp).Row
For lngCounter = 2 To lngLast
With Cells(lngCounter, "B")
For Each rCell In RefSheet.Range("B1", RefSheet.Cells(RefSheet.Rows.Count, 1)).Cells
lCnt = lCnt + 1
'I wasn't sure what to put as a reference to
If .Value = "" Then
.Interior.ColorIndex = 6
End If
Next rCell
End With
Next lngCounter
Application.ScreenUpdating = True
End Sub
在第五次閱讀時,我開始意識到這個模糊的想法,即您想將無序的Sheet ALL!(Location1,Location2)與一張無序的Sheet RefSheet!(Location1,Location2)對匹配。那是對的嗎? –
是的。必須有兩個循環。一個通過主循環,另一個通過RefSheet – user1442625