简体   繁体   中英

How to highlight selected cells in the same row within a range

what i am trying to do here is, when ever the column "g" have a empty cell, it will highlight the value in column E in the same row. so far i have got is when ever the column "g" have empty cell it highlight the entire row. I also want to range the highlight to the last row. I couldn't do that. Please help me out.

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

I slightly different approach, without using Range or Select . I couldn't quite understand what you wanted to highlight though. If you make it more clear, I can adjust the example...

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

You may try something like this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM