Highlight cells Data

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good morning

I am trying to highlight cells with data in non contiguous columns. Below is the code I have started with, not sure what I am doing incorrectly.

Thank for any help.

Code:
Sub HighlightCellwithData2()    
    Dim i As Long, lr As Long
    
    lr = Range("H" & Rows.Count).End(xlUp).Row
    
    For i = 2 To lr
    
    If Range("A" & i) <> "" Then
    
    Range("A" & i).Interior.Color = RGB(218, 239, 245)
    
        If Range("H" & i) <> "" Then
    
    Range("H" & i).Interior.Color = RGB(218, 239, 245)
    
    End If
    
        End If
        
            
    
    Next
    
        
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Could we do it all at once with Conditional Formatting like this?
Code:
Sub HighlightData()
  Dim lr As Long
  
  lr = Range("H" & Rows.Count).End(xlUp).Row
  With Range("A2:A" & lr & " ,H2:H" & lr)
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=" & .Cells(1, 1).Address(0, 0) & "<>"""""
    .FormatConditions(1).Interior.Color = RGB(218, 239, 245)
  End With
End Sub
 
Upvote 0
Mr Peter Thank You. This works as requested.

If I may if there are additional columns needed how could I include them?
 
Upvote 0
If I may if there are additional columns needed how could I include them?
The most efficient way depends on whether they are all disjoint, or perhaps some of them are joined, but the basic concept would be to add more sections like the coloured one below.

Rich (BB code):
  With Range("A2:A" & lr & " ,H2:H" & lr)
 
Last edited:
Upvote 0
Came up with another alternative, not sure if this is as efficient as it could be. The issue I believe I am having is the data in column A is not as long as Column H.

Maybe someone has a better idea...

Code:
[TABLE="width: 72"]
 <colgroup><col width="72" style="width:54pt"> </colgroup><tbody>[TR]
  [TD="class: xl63, width: 72"]    Dim Finalrow As Long, Finalrow1 As Long,  Finalrow2 As Long[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    Finalrow = Cells(Rows.Count,  "A").End(xlUp).Row[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    Finalrow1 = Cells(Rows.Count,  "D").End(xlUp).Row[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    Finalrow2 = Cells(Rows.Count,  "H").End(xlUp).Row[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    With Range("A2:A" &  Finalrow)[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    .Interior.Color = RGB(218, 239, 245)[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    End With[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    With Range("D2:D" &  Finalrow1)[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    .Interior.Color = RGB(217, 238, 208)[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    End With[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    With Range("H2:H" &  Finalrow2)[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    .Interior.Color = RGB(255, 255, 204)[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    End With[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    [/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]    End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Your code now appears to be colouring all cells in the column down to the last entry, whether they are blank or not. Previously, your code was only highlighting cells that actually contained data.
Has the requirement actually changed?
 
Upvote 0
No it has not. I am unable to add a third range. Thought this may be a solution.

Thank you.
 
Last edited:
Upvote 0
No it has not.
Then it is as I said before, a case of adding more sections like the red one

Rich (BB code):
  With Range("A2:A" & lr & " ,H2:H" & lr)
So, to add columns C and G it would become
Rich (BB code):
With Range("A2:A" & lr & " ,H2:H" & lr & " ,C2:C" & lr & " ,G2:G" & lr)

However, if your columns of data are different in length and column H is not always the column to use to determine the last row, you could change how that is calculated.
Rich (BB code):
<del>lr = Range("H" & Rows.Count).End(xlUp).Row</del>
lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, SearchFormat:=False).Row
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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