如果A1不等于空白,则VBA颜色第1行,依此类推

时间:2017-01-24 16:28:06

标签: vba excel-vba colors excel

我正在尝试遍历第1行并根据单元格是否为空白对其进行着色。例如,如果某些东西在A1中,则将其着色为浅灰色,如果某些内容在单元格B1中,则颜色相同。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果活动表的A1中有文本,则可以使用类似的内容:

Sub test()
Dim c As Integer
c = Application.WorksheetFunction.CountA(ActiveSheet.Range("A1"))
If c > 0 Then
    ActiveSheet.Range("A1").Interior.ColorIndex = 15
End If
End Sub

答案 1 :(得分:0)

如果表达式为true,则会将行着色为绿色。您将颜色指定为RGB。

=ColorRowIF(A1=B1, 1, 181, 0)

或者在您的情况下,条件为ISBLANK

=ColorRowIF(ISBLANK(B2), 1, 181, 0)

Public Function ColorRowIF(Condition As Boolean, r As Integer, g As Integer, b As Integer) As String
    Dim row As Integer
    row = Application.Caller.row

    If Condition = True Then
        ActiveSheet.Evaluate "ColorRow(" & row & ", " & r & ", " & g & ", " & b & ")"
    Else
        'ws.Rows(r).Interior.Color = vbRed
    End If

    ColorRowIF = Condition
End Function


Public Sub ColorRow(row As Integer, r As Integer, g As Integer, b As Integer)

    Dim ws As Worksheet
    Set ws = ActiveSheet
    ws.Rows(row).Interior.Color = RGB(r, g, b)

End Sub


Function IsDate(CellDate As Date) As Boolean
    If CellDate >= 1 And CellDate <= #12/31/2199# Then
    ' 1 is equal to January 1, 1900
        IsDate = True
    Else
        IsDate = False
        End If
End Function