突出显示VBA中的整行

时间:2015-08-03 01:08:46

标签: vba excel-vba excel

我有这张桌子:

        ID CODE     NAME    Percentage  unit    sales
         6494      Orange      0%        2      74.00 
          582      apple       0%        1      90.00 
SubTotal                       1%        3     164.00 
         6493      Banana     171%      951    25,677.00 
         6494    Strawberry    33%      182    6,734.00 
SubTotal                       204%     1133   32,411.00 
Grand Total                    205%     1136   32,575.00 

我希望突出显示所有单元格,直到包含单词" TOTAL"的最后一列。

此代码仅突出显示包含" Total"的单元格。如何突出显示整行?

MySearch = Array("Total")
myColor = Array("3")
For Each sh In ActiveWorkbook.Worksheets

    With sh.Cells
        .Interior.ColorIndex = xlColorIndexNone

        For I = LBound(MySearch) To UBound(MySearch)

            Set Rng = .Find(What:=MySearch, After:=ActiveSheet.Range("J8"), LookIn:=xlValues, MatchCase:=False)
            If Not Rng Is Nothing Then
                FirstAddress = Rng.Address
                Do
                    Rng.Interior.ColorIndex = myColor(I)
                    Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
        Next I
    End With
Next sh

1 个答案:

答案 0 :(得分:0)

在设置颜色之前,更改设置ColorIndex的行以选择整行。试试这个: -

'This selects the whole line, based on the resultant "Rng" cell returned by "Find".
'It uses CurrentRegion to determine number of columns in the vicinity.
Range(Rng, Cells(Rng.Row, Rng.Column + Rng.CurrentRegion.Columns.Count - 1)).Interior.ColorIndex = myColor(I)