[英]Color cells based on their value - VBA
我需要能夠根據VBA中的值對單元格進行顏色編碼,因為條件格式將無法處理最終需要的條件數量。 例如,如果B的值是“ Decommission”,那么我想檢查C,D和E的值,並根據B的值對它們進行顏色編碼。不幸的是,我編寫的代碼貫穿整個過程表格和顏色會根據第一個值對范圍內的所有內容進行編碼。 我已經定義了2個范圍(all_data和response),我知道出了什么問題,但是我不知道如何告訴代碼僅將格式限制為行的值。任何幫助將不勝感激。
Sub Formatter()
Dim All_Data As Range
Dim Response As Range
Dim MyCell As Range
Dim MyCell2 As Range
Set All_Data = Range("All_Data")
Set Response = Range("Response")
For Each MyCell In All_Data
If MyCell.Value = "Decommission" Then
MyCell.Interior.ColorIndex = 3
For Each MyCell2 In Response
If MyCell2.Value = "Yes" Then
MyCell2.Interior.ColorIndex = 4
End If
Next
End If
Next
End Sub
嘗試這個:
Sub Formatter()
Dim All_Data As Range
Dim Response As Range
Dim MyCell As Range
Dim MyCell2 As Range
Set All_Data = Range("All_Data")
Set Response = Range("Response")
For Each MyCell In All_Data
If MyCell.Value = "Decommission" Then
MyCell.Interior.ColorIndex = 3
For Each MyCell2 In Intersect(Response, ActiveSheet.Rows(MyCell.Row))
If MyCell2.Value = "Yes" Then
MyCell2.Interior.ColorIndex = 4
End If
Next
End If
Next
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.