[英]Conditional formatting cells if part of their value equals ANY value of another column
[英]Conditionally formatting cells if their value equals any value of another column
我在A
和B
列中有數據。 B
列的數據大部分是A
數據的重復,但並非總是如此。 例如:
A
Budapest
Prague
Paris
Bukarest
Moscow
Rome
New York
B
Budapest
Prague
Los Angeles
Bukarest
我需要在A
列中搜索B
的值。 如果一行匹配,我需要將A
中行的背景顏色更改為紅色或其他顏色。
這是公式
基於公式以條件格式創建新規則。 使用以下公式並將其應用於$A:$A
=NOT(ISERROR(MATCH(A1,$B$1:$B$1000,0)))
更新
這是@pnuts的建議,它也很完美:
=MATCH(A1,B:B,0)>0
另一個更簡單的解決方案是在條件格式中使用此公式(適用於 A 列):
=COUNTIF(B:B,A1)
問候!
您需要做的只是一個簡單的循環。
這不處理小寫、大寫不匹配的測試。 如果這不是您正在尋找的內容,請發表評論,我可以修改。
如果您打算學習VBA。 這是一個很好的開始。
測試:
Sub MatchAndColor()
Dim lastRow As Long
Dim sheetName As String
sheetName = "Sheet1" 'Insert your sheet name here
lastRow = Sheets(sheetName).Range("A" & Rows.Count).End(xlUp).Row
For lRow = 2 To lastRow 'Loop through all rows
If Sheets(sheetName).Cells(lRow, "A") = Sheets(sheetName).Cells(lRow, "B") Then
Sheets(sheetName).Cells(lRow, "A").Interior.ColorIndex = 3 'Set Color to RED
End If
Next lRow
End Sub
我正在研究這個並喜歡使用 for 循環的 peege 方法! (因為我現在正在學習 VBA)
但是,如果我們試圖匹配另一列的“任何”值,那么使用如下嵌套循環如何?
Sub MatchAndColor()
Dim lastRow As Long
Dim sheetName As String
sheetName = "Sheet1" 'Insert your sheet name here
lastRow = Sheets(sheetName).Range("A" & Rows.Count).End(xlUp).Row
For lRowA = 1 To lastRow 'Loop through all rows
For lRowB = 1 To lastRow
If Sheets(sheetName).Cells(lRowA, "A") = Sheets(sheetName).Cells(lRowB, "B") Then
Sheets(sheetName).Cells(lRowA, "A").Interior.ColorIndex = 3 'Set Color to RED
End If
Next lRowB
Next lRowA
End Sub
我無法對最佳答案發表評論,但 Excel 實際上可以讓您在不添加丑陋條件邏輯的情況下執行此操作。
條件格式會自動應用於任何不是錯誤的輸入,因此您可以獲得與以下相同的效果:
=NOT(ISERROR(MATCH(A1,$B$1:$B$1000,0)))
有了這個:
= MATCH(A1,$B$1:$B$1000,0)))
如果將上述內容應用於您的數據,如果 A1 與 $B$1:$B$1000 中的任何單元格匹配,則將對其進行格式化,因為任何不匹配都會返回錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.