简体   繁体   中英

Coloring a cell within a range, if the same cell within an identical range on a different worksheet is already colored?

In VBA, I am flagging results within a range and coloring them green based on cell value (ex. value < "28"). Each sheet (of four total) corresponds to a different marker and is flagged green based on a value. All the sheets have an identical X and Y axis, with the range of interest being identical between sheets (B2:BJ26). I would like to make a fifth sheet that colors the corresponding cell green if all four other corresponding cells from the other sheets are colored green.

I could do this cell by cell....

Simplified example

If Sheets(A) "B2" value < 30 AND Sheets(B) "B2" Value > 1.1 AND Sheets(C) "B2" Value < 1500 AND Sheets(D) "B2" Value > 0.30 THEN Sheets(E) "B2" interior.color = RGB(0,255,0) 

But there must be a more efficient way for all cells within the B2:BJ26 range. Someone more skilled, please help me.

Example of working code I'm using to color/flag values on the first four sheets.

Worksheets("Sheet 1").Activate

Dim XXXXXXX As Range, cell As Range
Set XXXXXXX = Range("B2:BJ26")

For Each cell In XXXXXXX

If cell.Value < "28" And cell.Value > "1" Then
        cell.Interior.Color = RGB(0, 255, 0)
    End If

Next

This code did the job for me. Let me know if it gave you the results you expected.

Option Explicit

Sub ColorSheetFive()
    Dim i As Integer
    Dim m As Integer
    Dim n As Integer
    Dim allGreen As Boolean

    For m = 2 To 26
        For n = 2 To 62
            allGreen = True
            For i = 1 To 4
                If Sheets(i).Cells(m, n).Interior.Color <> RGB(0, 255, 0) Then
                    allGreen = False
                End If
            Next i
            If allGreen Then
                Sheets(5).Cells(m, n).Interior.Color = RGB(0, 255, 0)
            End If
        Next n
    Next m

    MsgBox "Color checking complete!"

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM