如果使用VBA应用某些条件,则为Excel行着色

时间:2017-12-11 20:31:03

标签: excel vba

如果某些条件适用,我试图制作一个宏来为excel行着色,但是,当我运行它时,我在这一行中遇到语法错误:

If (Not item1 (Cells(matchline, 1)) Then GoTo continue

另外,我希望某个范围有颜色,而不是整行。我从另一个宏中获得了这个,但不知道如何在ColorRows中正确应用它:

Range(Cells(Rng.row, "A"), Cells(Rng.row, "M")).Interior.Color = xlNone

当前代码:

Option Explicit

Sub ColorRows()


    Dim matchline As Integer, lastmatchline As Integer, lastbinline As Integer
    Dim item1 As String, line As Integer, endline As Integer

    'For line = 3 To endline

    For matchline = 6 To lastmatchline

        item1 = Cells(matchline, 1).Value

        If (Not item1 (Cells(matchline, 1)) Then GoTo continue

        If Not item1(Cells(matchline, 1)) Then GoTo continue

        If (item1 = "Unexpected Status") Then _
        Cells(matchline, 1).EntireRow.Font.Interior.Color = 13434828

        If (item1 = "At Risk") Then _
        Cells(matchlineline, 1).EntireRow.Font.Interior.Color = 8420607

        If (item1 = "Requirements Definition") Then _
        Cells(matchlineline, 1).EntireRow.Font.Interior.Color = 10092543

continue:
    Next line
End Sub

1 个答案:

答案 0 :(得分:0)

尝试类似:

Dim ws As Worksheet
Dim rows As Long, i As Long
Dim rngSearch As Range, rngColor As Range

Application.ScreenUpdating = False
Application.EnableEvents = False

Set ws = ActiveSheet

rows = ws.UsedRange.rows.Count

For i = 1 To rows
    Set rngSearch = ws.Cells(i, 1)
    Set rngColor = ws.Range("A" & i, "M" & i)

    If rngSearch = "Unexpected Status" Then
        rngColor.Interior.Color = 13434828
    End If
    If rngSearch = "At Risk" Then
        rngColor.Interior.Color = 8420607
    End If
    If rngSearch = "Requirements Definition" Then
        rngColor.Interior.Color = 10092543
    End If
Next i

Application.ScreenUpdating = True
Application.EnableEvents = True