如何突出显示范围内同一行中的选定单元格

时间:2017-07-27 12:46:34

标签: excel vba excel-vba

我在这里尝试做的是,当“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

2 个答案:

答案 0 :(得分:0)

我的方法略有不同,没有使用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

答案 1 :(得分:0)

您可以尝试这样的事情......

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