如果某些条件适用,请使用VBA为Excel行着色

如果某些条件适用,请使用VBA为Excel行着色,excel,vba,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 当前代码: Op

如果某些条件适用,我尝试创建一个宏来为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
尝试以下方法:

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

这肯定是不正确的语法,但是,由于不知道您在该行中尝试测试什么,因此很难建议您应该将其更改为什么。您的下一个匹配行是否也应该是下一个匹配行?^^^或者只是
Next
,lastmatchline初始化为0,但您没有后退(例如,步骤-1)你从6点到0点怎么样?嗨,布兰登,谢谢你的提示。成功了!但是,我希望单元格是彩色的,而不是字体。我试图将您的建议改为:rngColor.Font.Interior.Color,但后来它不再起作用。希望你能帮助我更新代码以反映变化。将Font.Color更改为Interior.Color。