【问题标题】:How to highlight selected cells in the same row within a range如何突出显示范围内同一行中的选定单元格
【发布时间】:2017-07-27 20:58:28
【问题描述】:

我在这里要做的是,当“g”列有一个空单元格时,它将突出显示同一行中 E 列中的值。到目前为止,我得到的是,当“g”列有空单元格时,它会突出显示整行。我还想将突出显示范围到最后一行。我不能那样做。请帮帮我。

Sub highlightRow(ByVal comp_workbook As Workbook)
comp_workbook.Sheets(1).Select
Dim EmptyCell As Range
Range("G:G").Select
For Each EmptyCell In Selection
    If EmptyCell = "" Then EmptyCell.EntireRow.Interior.ColorIndex = 43
Next EmptyCell
End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我的方法略有不同,没有使用RangeSelect。我不太明白你想强调什么。如果你说的更清楚,我可以调整例子......

    Sub HighlightCells()
        Dim rowStart As Long
        Dim rowEnd As Long
        Dim colToCheck As String
        Dim colToHighligt As String
    
        'Change variablesto fit your requirement
        rowStart = 1
        rowEnd = 100
        colToCheck = "G"
        colToHighlight = "E"
    
        'Highlights cell in column E, if the cell in column G is empty
        For i = rowStart To rowEnd
            If IsEmpty(Cells(i, colToCheck)) Then
                    Cells(i, colToHighlight).Interior.ColorIndex = 43
            End If
        Next i
    
    End Sub
    

    【讨论】:

    • 谢谢你们,你们是最棒的。非常感谢。
    【解决方案2】:

    你可以试试这样的……

    Sub HighlightCells(ByVal comp_workbook As Workbook)
    Dim ws As Worksheet
    Dim lr As Long
    Application.ScreenUpdating = False
    Set ws = comp_workbook.Sheets(1)
    lr = ws.UsedRange.Rows.Count
    With ws.Rows(1)
        .AutoFilter field:=7, Criteria1:=""
        If ws.Range("E1:E" & lr).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
            ws.Range("E2:E" & lr).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 43
        End If
        .AutoFilter
    End With
    Application.ScreenUpdating = True
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2022-01-27
      • 2014-02-03
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多