"下标超出范围"尝试根据单元格更改文本颜色时出错值

时间:2017-07-20 18:54:33

标签: excel vba excel-vba compiler-errors runtime-error

我正在尝试创建一个宏,用于比较标记为"申请单号"的两列中的单元格值。和#34; PO#"到前一行的单元格。如果单元格相同,我希望线条突出显示相同的颜色。以下是我的代码:

Sub changeTextColor()

    Dim Color As Integer

    Color = 5

    'Get number of rows in the specified column
    RowsCount = Range("A1", Range("A1").End(xlDown)).Rows.Count

    Dim colReq As Range
Dim colPO As Range


With ActiveSheet.UsedRange.Rows(1)
    Set colReq = .Find(What:="Requisition Number", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    Set colPO = .Find(What:="PO #", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
End With


    'Select cell
    ActiveSheet.Cells(1, colReq.Column).Select

    'Loop the cells
    For x = 1 To RowsCount
        If (ActiveCell.Value = ActiveSheet.Cells(ActiveCell.Row + 1, ActiveCell.Column).Value) And _
            ActiveSheet.Cells(ActiveCell.Row, colPO.Column).Value = ActiveSheet.Cells(ActiveCell.Row+1, colPO.Column).Value Then

                ActiveCell.EntireRow.Font.ColorIndex = Color
                ActiveCell.EntireRow+1.Font.ColorIndex = Color

        Else
            Color = Color + 5
        End If

        ActiveCell.Offset(1, 0).Select
    Next

End Sub

我收到错误"下标超出范围"在我的代码的以下行中,我不知道如何解决它?

           ActiveCell.EntireRow.Font.ColorIndex = Color
           ActiveCell.EntireRow+1.Font.ColorIndex = Color

1 个答案:

答案 0 :(得分:0)

这主要是语法错误。 ActiveCell.EntireRow + 1.Font.ColorIndex = Color卓越没有意义。导致问题的是EntireRow + 1。您必须使用偏移量,就像我在下面的代码中看到的那样。

试试这个:

            ActiveCell.EntireRow.Font.ColorIndex = Color
            ActiveCell.offset(1,0).Select
            ActiveCell.EntireRow.Font.ColorIndex = Color

就像@brucewayne在评论中所说,你真的想避免尽可能多地选择/激活细胞。它会减慢您的代码速度并且可能很笨拙。

以下是一篇关于如何以及为何避免使用select和activate https://www.excelcampus.com/vba/how-to-avoid-the-select-method/

的文章

for循环的一个强大功能是,您可以在代码中使用计数器变量(在您的情况下为'x')来代替偏移量。我已经使用几种不同的技术取出了你的代码中的select和activate。请注意我如何在循环中使用cells(x,col)为每次迭代向下移动一个单元格,以及如何使用cells(x + 1,col)使其低于x的值。 (在测试代码之前,请务必将“工作表名称”替换为工作表的实际名称)

Sub changeTextColor()

Dim Color As Integer
Dim colReq As Range
Dim colPO As Range

Color = 5

    'Get number of rows in the specified column
RowsCount = Range("A1", Range("A1").End(xlDown)).Rows.Count



With Worksheets("Sheet Name").UsedRange.Rows(1)
    Set colReq = .Find(What:="Requisition Number", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    Set colPO = .Find(What:="PO #", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
End With



    'Loop the cells
    For x = 1 To RowsCount
        If (Worksheets("Sheet Name").Cells(x, colReq.Column).Value = Worksheets("Sheet Name").Cells(x + 1, colReq.Column).Value) And _
            Worksheets("Sheet Name").Cells(x, colPO.Column).Value = Worksheets("Sheet Name").Cells(x + 1, colPO.Column).Value Then

                Worksheets("Sheet Name").Cells(x, colReq.Column).EntireRow.Font.ColorIndex = Color
                Worksheets("Sheet Name").Cells(x + 1, colReq.Column).EntireRow.Font.ColorIndex = Color
        Else
            Color = Color + 5
        End If
    Next

End Sub