VBA程序无法正常运行

时间:2017-11-22 00:16:01

标签: excel-vba vba excel

我在excel中设置了一个VBA程序,当给定行中的几个单元格的条件彼此相等时,它将突出显示一行中的单元格。 (参见下面的代码)调试器上没有出现错误,指定的单元格也没有突出显示。

在下面的示例数据中,程序将突出显示案例1行(自1 = 1)和(10 = 10)的单元格。关于如何使这个工作的任何建议?

Sub x()

Dim r As Integer
Dim c As Integer


With Worksheets(1)

For r = 1 To .Range("B" & Rows.Count).End(xlUp).row
    c = .Cells(r, Columns.Count).End(xlToLeft).Column

    If Cells(r, 3).Value = Cells(r, 6).Value & Cells(r, 4).Value = Cells(r, 7).Value Then
        Cells(r, 13).Interior.ColorIndex = 46
        Application.ScreenUpdating = True

        End If
Next r

End With

End Sub

示例数据:

Case_1   HT 1   10  STD 1   10  0.008893    0.02564 71  0.35    0.7297
Case_2   HT 1   10  HT  1   15  -0.04434    0.02564 71  -1.73   0.0881
Case_3  HT  1   10  STD 1   15  0.008893    0.02564 71  0.35    0.7297

1 个答案:

答案 0 :(得分:0)

以上所有评论都很好,但我想我会把它们放在一起 你需要改变“&”到“和”。
去掉; “Dim c”行,“c = Cells”行和“Application.ScreenUpdating”行 我正在运行Excel 2010;您的代码使用上述更改,使用“If”语句中的“.Cells”或“Cells” 我还从每个Cell引用中删除了“.Value”,它可以使用或不使用它。

Dim r As Integer
'Dim c As Integer

    With Worksheets(1)

    For r = 1 To Range("B" & Rows.Count).End(xlUp).Row
        'c = Cells(r, Columns.Count).End(xlToLeft).Column

        If Cells(r, 3) = Cells(r, 6) And Cells(r, 4) = Cells(r, 7) Then
            Cells(r, 13).Interior.ColorIndex = 46
            'Application.ScreenUpdating = True

        End If
    Next r

    End With
相关问题