Color the cells that contain a value in a specific row

sofas

Active Member
Joined
Sep 11, 2022
Messages
493
Office Version
  1. 2019
Platform
  1. Windows
Hello, I want to search in row No. 2 of the worksheet, and when a cell with a value is found, the background of the cell is colored yellow and the empty cells are ignored.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi sofas,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    'https://www.mrexcel.com/board/threads/color-the-cells-that-contain-a-value-in-a-specific-row.1258162

    Dim ws As Worksheet
    Dim rngFound As Range
    Dim strFirstAddress As String, strMyText As String
    
    Application.ScreenUpdating = False
    
    strMyText = "Hello World!!" 'Text to be highlighted. Change to suit.
    
    Set ws = ThisWorkbook.Sheets(CStr("Sheet1")) 'Sheet name with the data in Row 2 to be checked. Change to suit.
        Set rngFound = ws.Rows("2:2").Find(What:=strMyText, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not rngFound Is Nothing Then
            rngFound.Interior.Color = RGB(255, 255, 0)
            strFirstAddress = rngFound.Address
            Do
                Set rngFound = ws.Rows("2:2").FindNext(rngFound)
                rngFound.Interior.Color = RGB(255, 255, 0)
            Loop While Not rngFound Is Nothing And rngFound.Address <> strFirstAddress
        End If
    Set rngFound = Nothing
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 1
Hi sofas,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    'https://www.mrexcel.com/board/threads/color-the-cells-that-contain-a-value-in-a-specific-row.1258162

    Dim ws As Worksheet
    Dim rngFound As Range
    Dim strFirstAddress As String, strMyText As String
   
    Application.ScreenUpdating = False
   
    strMyText = "Hello World!!" 'Text to be highlighted. Change to suit.
   
    Set ws = ThisWorkbook.Sheets(CStr("Sheet1")) 'Sheet name with the data in Row 2 to be checked. Change to suit.
        Set rngFound = ws.Rows("2:2").Find(What:=strMyText, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not rngFound Is Nothing Then
            rngFound.Interior.Color = RGB(255, 255, 0)
            strFirstAddress = rngFound.Address
            Do
                Set rngFound = ws.Rows("2:2").FindNext(rngFound)
                rngFound.Interior.Color = RGB(255, 255, 0)
            Loop While Not rngFound Is Nothing And rngFound.Address <> strFirstAddress
        End If
    Set rngFound = Nothing
   
    Application.ScreenUpdating = True
   
End Sub

Regards,

Robert
Thanks, I tried the code works well, but I don't have a specific number or word to search. What I want is to color any cell with a value and ignore the empty along row 2
 
Upvote 0
What I want is to color any cell with a value and ignore the empty along row 2

Try this:

VBA Code:
Option Explicit
Sub Macro2()

    'https://www.mrexcel.com/board/threads/color-the-cells-that-contain-a-value-in-a-specific-row.1258162

    Dim ws As Worksheet
    Dim rngFound As Range
    Dim strFirstAddress As String
   
    Application.ScreenUpdating = False
   
    Set ws = ThisWorkbook.Sheets(CStr("Sheet1")) 'Sheet name with the data in Row 2 to be checked. Change to suit.
        Set rngFound = ws.Rows("2:2").Find(What:="*", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not rngFound Is Nothing Then
            rngFound.Interior.Color = RGB(255, 255, 0)
            strFirstAddress = rngFound.Address
            Do
                Set rngFound = ws.Rows("2:2").FindNext(rngFound)
                rngFound.Interior.Color = RGB(255, 255, 0)
            Loop While Not rngFound Is Nothing And rngFound.Address <> strFirstAddress
        End If
        Set rngFound = Nothing
    Set ws = Nothing
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Option Explicit
Sub Macro2()

    'https://www.mrexcel.com/board/threads/color-the-cells-that-contain-a-value-in-a-specific-row.1258162

    Dim ws As Worksheet
    Dim rngFound As Range
    Dim strFirstAddress As String
  
    Application.ScreenUpdating = False
  
    Set ws = ThisWorkbook.Sheets(CStr("Sheet1")) 'Sheet name with the data in Row 2 to be checked. Change to suit.
        Set rngFound = ws.Rows("2:2").Find(What:="*", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not rngFound Is Nothing Then
            rngFound.Interior.Color = RGB(255, 255, 0)
            strFirstAddress = rngFound.Address
            Do
                Set rngFound = ws.Rows("2:2").FindNext(rngFound)
                rngFound.Interior.Color = RGB(255, 255, 0)
            Loop While Not rngFound Is Nothing And rngFound.Address <> strFirstAddress
        End If
        Set rngFound = Nothing
    Set ws = Nothing
  
    Application.ScreenUpdating = True
  
End Sub
Thank you very much I appreciate your help
 
Upvote 0

Forum statistics

Threads
1,216,037
Messages
6,128,439
Members
449,453
Latest member
jayeshw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top