ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Highlght rows (https://www.excelbanter.com/excel-programming/406756-highlght-rows.html)

Alan M

Highlght rows
 
I have a column containing a collection of various numbers. I need to use
code to highlight the rows containing the number 5.

I have tried with this but it does not work.

For Each Cell In Columns(2)

If Cell.Value = "5" Then
EntireRow.Select

With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
End If

Not sure why....Help please?


JE McGimpsey

Highlght rows
 
It's probably not working because your comparison looks for text and
your cells contain numbers.

Try:

Dim rCell As Range
For Each rCell In Range("B1:B" & _
Range("B" & Rows.Count).End(xlUp).Row)
With rCell
If IsNumeric(.Value) Then
If .Value = 5 Then
With .EntireRow.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
End If
End If
End With
Next rCell

Note that there's no need to select anything.


In article ,
Alan M wrote:

I have a column containing a collection of various numbers. I need to use
code to highlight the rows containing the number 5.

I have tried with this but it does not work.

For Each Cell In Columns(2)

If Cell.Value = "5" Then
EntireRow.Select

With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
End If

Not sure why....Help please?


John

Highlght rows
 
this may work but assumes no blank rows in range you are checking:

For Each rw In Worksheets(1).Cells(1, 1).CurrentRegion.Rows
check = rw.Cells(1, 2).Value
If check = 5 Then rw.EntireRow.Interior.ColorIndex = 15
Next
--
JB


"Alan M" wrote:

I have a column containing a collection of various numbers. I need to use
code to highlight the rows containing the number 5.

I have tried with this but it does not work.

For Each Cell In Columns(2)

If Cell.Value = "5" Then
EntireRow.Select

With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
End If

Not sure why....Help please?


Don Guillett

Highlght rows
 
sub colorrows()
mc=2
for i=1 to cells(rows.count,mc).end(xlup).row
if cells(i,mc)=5 then rows(i).interior.colorindex=15
next i
end sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Alan M" wrote in message
...
I have a column containing a collection of various numbers. I need to use
code to highlight the rows containing the number 5.

I have tried with this but it does not work.

For Each Cell In Columns(2)

If Cell.Value = "5" Then
EntireRow.Select

With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
End If

Not sure why....Help please?




All times are GMT +1. The time now is 07:03 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com