【问题标题】:Highlighting a the used cells in a particular row突出显示特定行中使用的单元格
【发布时间】:2015-12-31 02:01:17
【问题描述】:

我正在为工作中的文件编写 VBA,需要做一些奇怪的事情。如果列 J 中的单元格包含某个值,我需要突出显示该行(不是整行,只是行的已使用部分)。除了我的代码突出显示整行之外,我已经弄清楚了一切,我只希望它突出显示该行中使用的单元格。任何人都可以建议吗?代码如下

'Yellow Highlight..........THIS IS HIGHLIGHTING THE WHOLE ROW....WHY!!!!! WHY!!!!!!!!!!!
Sheets("EMM").Activate
    With Sheets("EMM")
        For Lrow = 1 To ActiveSheet.UsedRange.Rows.Count
                With .Cells(Lrow, "J")
                    If Not IsError(.Value) Then
                        If .Value = "Desk to adjust" Then
                            .EntireRow.Interior.ColorIndex = 6
                        End If
                    End If
                End With
        Next Lrow
    End With

【问题讨论】:

  • 因为 .EntireRow 将颜色应用于整行...您将检查每一列并为行的单元格着色而不是整行..
  • .EntireRow.Interior.ColorIndex = 6 的 EntireRow 导致整行突出显示。要突出显示特定单元格,请使用 Range("A1").Interior.Color = RGB(255, 0, 0)

标签: excel vba highlight


【解决方案1】:
With Sheets("EMM")
    For Lrow = 1 To .UsedRange.Rows.Count
        With .Cells(Lrow, "J")
            If Not IsError(.Value) Then
                If .Value = "Desk to adjust" Then
                    Sheets("EMM").UsedRange.Rows(Lrow).Interior.ColorIndex = 6
                End If
            End If
        End With
    Next
End With

或使用自动过滤器

With Sheets("EMM").UsedRange
    .Parent.AutoFilterMode = False
    .AutoFilter
    .AutoFilter Field:=10, Criteria1:="Desk to adjust"
    .Rows(1).Hidden = True      'Header row
    .Interior.ColorIndex = 6
    .Rows(1).Hidden = False     'Header row
    .AutoFilter
    .Cells(1, 1).Activate
End With

【讨论】:

    【解决方案2】:

    避免在行上循环并获取非空白单元格或设置过滤器的最简单方法是在 IF 中执行此操作:

    .UsedRange.Rows(lRow).Interior.ColorIndex = 6
    .UsedRange.Rows(lRow).SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 0
    

    (突出显示该行然后取消突出显示空单元格)

    【讨论】:

      【解决方案3】:

      这里是一些注释代码,展示了如何使用 Range.AutoFilter 方法来实现您正在寻找的结果。不需要循环,因此通常效率更高:

      Sub tgrFilter()
      
          Dim ws As Worksheet
          Dim rngData As Range
          Dim strMatch As String
      
          Set ws = ActiveWorkbook.Sheets("EMM")       'We will be working with sheet "EMM" in activeworkbook
          Set rngData = ws.Range("J1").CurrentRegion  'Get the region that column J is a part of in order to limit the highlighting (so it doesn't highlight entire row)
          strMatch = "Desk to adjust"                 'This is the string/value we are looking for in column J
          rngData.Interior.Color = xlNone             'Clear any previous highlighting
      
          On Error Resume Next    'If there are no results, it would cause an error
      
          'Work with just column J within rngData
          With Intersect(rngData, ws.Columns("J"))
              .AutoFilter 1, strMatch     'Note that the filter is not case sensitive
      
              'Color the matching rows contained in the data (not the entire row)
              Intersect(rngData, .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow).Interior.ColorIndex = 6
      
              .AutoFilter     'Remove the filter
          End With
      
          If Err.Number <> 0 Then
              'Error occurred which means there were no results
              MsgBox "No matches found in column J for [" & strMatch & "]", , "No results"
              Err.Clear
          End If
      
          On Error GoTo 0         'Remove the On Error Resume Next condition
      
      End Sub
      

      【讨论】:

        【解决方案4】:

        您可以使用条件格式代替宏。

        选择您需要突出显示的单元格,然后在菜单栏中转到格式 -> 条件格式

        在你需要检查的对话框选择条件中,这个例子检查单元格值是否等于'AA'。您一次最多可以添加 3 个条件

        下一步,当条件为真时,点击格式化按钮格式化单元格。

        全部完成后单击确定关闭对话框,您将获得所需的突出显示。

        【讨论】:

          猜你喜欢
          • 2020-03-29
          • 2017-10-29
          • 2011-04-12
          • 2015-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-29
          • 2013-07-09
          相关资源
          最近更新 更多