VBA - 如果三个单元格中有两个为真

时间:2017-08-23 16:40:03

标签: excel vba excel-vba

我正在尝试构建和if语句,如果三个单元格中的两个被着色,则变为选项卡Red;如果仅打开,则变为绿色。我希望有一种更简单的方法来纠正它,而不是像这样的三种语句。

Dim dateRng As String, num As Integer, j As Integer, irng As Range, frng As Range
dateRng = Sheets("Input Raw Data").Range("B" & counter + 2).Value
num = Sheets("Tool Setup").Range("C18").Value
NumPts = num * 3

For s = 1 To Sheets.Count
    With Sheets(s)
    For j = 1 To num
        If .Name = j Then
            .Range("A1:C1").Merge
            .Range("A1") = dateRng
            .Name = Sheets("Point Names").Range("B" & (3 * j - 1))
        End If
    Next j
    End With
Next s
For s = 1 to Sheets.Count
    With Sheets(s)
    For y = 1 To NumPts
        If .Name = Sheets("Reporting").Range("B" & (12 * y - 5)) Then
            For k = 6 To -1
                Set irng = Sheets("Reporting").Range("A" & (12 * y - k))
                Set irng = Sheets("Reporting").Range(irng, irng.End(xlToRight).End(xlToRight))
            irng.Copy (.Range("A2"))
            Next k
            .Columns("A:A").ColumnWidth = 12
            .Columns("B:B").EntireColumn.AutoFit
            If .Range("B7").Interior.ColorIndex > 0 Then
                a = 1
            End If

            If .Range("B8").Interior.ColorIndex > 0 Then
                a = a + 1
            End If

            If .Range("B9").Interior.ColorIndex > 0 Then
                a = a + 1
            End If

            If a >= 2 Then
                .Tab.ColorIndex = 3
            ElseIf a <= 1 Then
                .Tab.ColorIndex = 4
            End If
        End If
       y = y + 2
    Next y
    End With
Next s

1 个答案:

答案 0 :(得分:4)

这样的事可能会对你有所帮助。它仍然有多个if语句。但这些陈述很简单,不必处理不同细胞组合的着色方式。

另外,我使用colorindex > 0作为填充颜色的条件。

a = 0

If .Range("B7").Interior.ColorIndex > 0 Then
    a = 1
End If

If .Range("B8").Interior.ColorIndex > 0 Then
    a = a + 1
End If

If .Range("B9").Interior.ColorIndex > 0 Then
    a = a + 1
End If


If a = 2 Then
    .Range("B10").Interior.ColorIndex = 3
ElseIf a = 1 Then
    .Range("B10").Interior.ColorIndex = 43
End If