簡體   English   中英

帶有相交的Excel VBA宏條件格式

[英]Excel VBA Macro Conditional Formatting with Intersect

我編寫了一個Excel VBA宏來使用兩列的相交進行條件格式設置,但是由於某種原因我無法使其工作。 如果有人對我可以做什么進行修復有任何想法,我將不勝感激。

我想突出顯示匹配或重復的源列和目標列,如下所示:

E欄(目標)0.0000%0.0000%11.1803%12.7775%13.7190%13.9841%13.9841%14.5698%14.9071%15.5746%15.6492%16.1355%16.1355%16.3123%16.3123%19.0693%19.4511%21.9089%21.9089%21.9089%

V欄(來源)13.7190%14.9240%15.4919%20.4521%21.5725%23.3319%23.7718%24.1871%25.7257%27.2166%28.2290%29.7543%29.7543%30.4968%31.0080%31.9022%32.8570%33.3333%33.3333%34.7434%34.9603%34.9927%34.9927%36.4516 %36.8697%37.5637%38.2046%38.6151%38.7298%38.7298%39.3830%40.2694%41.8330%42.2049%

Sub Highlight_rsd_5batch()
Dim WatchRange As Range, Target As Range, cell As Range
Set Target = Range("E19:E237") 'change column ref as required
Set WatchRange = Range("V19:V237")

For Each cell In Target.Cells
If Intersect(Target, WatchRange) Is Nothing Then
cell.Interior.ColorIndex = xlNone
Else: cell.EntireRow.Interior.ColorIndex = 6
End If
Next cell
End Sub

Intersect功能檢查兩個范圍是否有共同的單元格,而不是是否有共同的值。 您可以改用CountIf函數:

Sub Highlight_rsd_5batch()
    Dim WatchRange As Range, Target As Range, cell As Range
    Set Target = Range("E19:E237") 'change column ref as required
    Set WatchRange = Range("V19:V237")

    For Each cell In Target.Cells
        If Application.WorksheetFunction.CountIf(WatchRange,cell.Value) > 0 Then
            cell.Interior.ColorIndex = 6
            For Each watchCell in WatchRange.Cells
                If watchCell.value = cell.Value Then: watchCell.Interior.ColorIndex = 6
            Next watchCell
        Else: cell.EntireRow.Interior.ColorIndex = xlNone
        End If
    Next cell
End Sub

此任務實際上不需要使用VBA,可以使用“格式”>“條件格式”下的“條件格式”工具中的相同公式來完成。 請參閱鏈接的教程以獲取更多幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM