【问题标题】:Highlight entire row in VBA在 VBA 中突出显示整行
【发布时间】:2015-08-03 12:08:48
【问题描述】:

我有这张桌子:

        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”。

此代码仅突出显示包含“总计”的单元格。如何突出显示整行?

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

【问题讨论】:

  • 请修正小计/总计的格式,以便更清楚地了解数据
  • 给你,我希望你能帮助我:)

标签: vba excel


【解决方案1】:

更改您设置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)

【讨论】:

  • 我在你的行中有这个错误,对象_Global的方法范围失败。我该怎么办?
  • 你声明了Rng吗? Dim Rng As Range
  • 听起来好像有一个空白工作表或每个工作表的数据不一致
猜你喜欢
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2016-03-27
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
相关资源
最近更新 更多