简体   繁体   中英

VBA search values in column and highlight matches in range

I'm having some trouble finding a way to highlight values in a range when that value exists in a certain column.

Column O has formulas which may populate values. Each of those values has it's own column starting in R . O10 could say "OD". Column S has a column Header in S8 that says "OD". Most of the cells below it are blank, but if they are not, they will say "OD", so since "OD" is found in Column O , I want All values not blank in column S to be .style = "Neutral".

Relevant Range for O needs to be O8 thru the last row. Columns to search for those values in will be R8:Last Column .

I have a decent start. I can get it to hightlight any non-blank values the way I want, but I can't get it to also make sure that value is in column O before it changes the style.

here's the code so far:

Sub HighlightCompGrid()

Dim FirstRow As Long, LastRow As Long, i As Long, x As Long, c As Range
Dim Selected As Range
Dim Grid As Range

FirstRow = Range("B:B").Find("ID", Range("B1")).Row
LastRow = Range("B:B").Find("End", Range("B8")).Row
i = Range("B" & FirstRow & ":B" & LastRow).Count

Set Grid = Range("R9", Range("R9").Offset(i - 1, i - 1))
Set Selected = Range("O9", Range("O9").Offset(i - 1, 0))

Selected.Select

For Each c In Grid
    If c.Value = Selected.Value Then
        Cells(c.Row, c.Column).Style = "Neutral"
    Else 'Do Nothing
    End If
Next c
End Sub

You don't need a macro for this. You can use an Excel feature called Conditional Formatting .

Conditional Formatting

Simply select your desired column in the worksheet, in the Ribbon go to Home -> Conditional Formatting -> Highlight Cells Rules -> Equal To...

As you can't just have nothing in the formula, just enter anything. Then alter the newly created rule by clicking Home -> Conditional Formatting -> Manage Rules

There, change it so that the rule is =""

Macro

If you really need a macro though, here's how I would do it:

Sub styleEmptyCells()

    Const FIRST_ROW        as Long = 2
    Const COLUMN_TO_CHECK  as String = "O"

    Dim i                  as Long 
    Dim lastrow            as Long

    lastrow = Range("A65535").End(xlUp).row

    For i = FIRST_ROW to lastrow
        If Range(COLUMN_TO_CHECK & i) = "" Then
           Range(COLUMN_TO_CHECK & i).Style = "Neutral" 
        End If
    Next

End Sub

I found some code online that I was able to modify to suit my need. It is SO SIMPLE in comparison to the crazy hoops I was trying to get through.

Here's what the code wound up looking like

Sub HighlightCompGrid()
Application.ScreenUpdating = False

TransposeNames
FillDownFormats
'these two macros will "reset" the grid

Dim FirstRow As Long, lastRow As Long, i As Long, c As Range
Dim Selected As Range
Dim Grid As Range

FirstRow = Range("B:B").Find("ID", Range("B1")).Row
lastRow = Range("B:B").Find("End", Range("B8")).Row
i = Range("B" & FirstRow & ":B" & lastRow).Count

Set Grid = Range("R9", Range("R9").Offset(i - 1, i - 1))
Set Selected = Range("O9", Range("O9").Offset(i - 1, 0))

For Each c In Grid
    If IsNumeric(Application.Match(c, Selected, 0)) Then
        c.Style = "Neutral"
        c.Borders.LineStyle = xlContinuous
    End If
Next c

Application.ScreenUpdating = False

End Sub

It loops through each value in Column O and checks for matches in my range of columns (Grid) and when they do... enter commands.

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