Cell Color is not changing for duplicate values
Hi All,
Please find the attached image.
In row 74,75 and 76 cells color is not highlighting. Excel going to row 47,48 and 49 and highlighting the color as both have same values.
In the below image when the loop reaches m value to 74 it automatically changing to 47 as it has the same value and highlighting the 47th row color.
Can anyone suggest me how can i highlight cell color even when i have duplicate values ?
Thanks,
Ramana
Attachment | Size |
---|---|
WBook.xls | 113 KB |
Quick comment, it looks like
Quick comment, it looks like you are comparing two strings
VBA provides a function for this, strcomp. Also string compares fail because of the prsence of non printable characters, so it is always worth doing a trim thus
strcomp(trim(range("A1").text),trim(range("A2").text)) = 0
is preferred syntax
Hi Phineas, yes.. you are
Hi Phineas,
yes.. you are right phineas. Always we should make sure to trim the text. but here the issue is different.
Regards,
Ramana
RE: Cell Color is not changing for duplicate values
Hi,
Here's some solution.
Firstly, declare the following variables:
' ************************* ' ************************* '
Dim strSoughtValue As String
Dim oTempRng As Range
Dim strFirstCell As String
' ************************* ' ************************* '
Now, replace the following part of your code:
' ************************* ' ************************* '
For m = 20 To .Range("F" & Rows.Count).End(xlUp).Row
If Trim(Imp_Rawdata.Range("F" & c.Row)) = Trim(.Range("F" & m)) Then
Debug.Print Imp_Rawdata.Range("F" & c.Row) & "\" & .Range("F" & m)
.Range("F" & m & ":Z" & m).Interior.Color = 5296274
Ref_exists = True
Exit For
End If
Next
' ************************* ' ************************* '
with this one:
' ************************* ' ************************* '
strSoughtValue = Trim(Imp_Rawdata.Range("F" & c.Row).Value)
With .Range("F20", .Range("F" & Rows.Count).End(xlUp))
Set oTempRng = .Find(What:=strSoughtValue, LookIn:=xlValues)
If Not oTempRng Is Nothing Then
Ref_exists = True
strFirstCell = oTempRng.Address
Do
oTempRng.Resize(ColumnSize:=21).Interior.Color = 5296274
Set oTempRng = .FindNext(oTempRng)
If oTempRng Is Nothing Then Exit Do
Loop While oTempRng.Address <> strFirstCell
End If
End With
' ************************* ' ************************* '
Best regards.
Hi Manny, Thanks a lot.. I
Hi Manny,
Thanks a lot.. I know the find function but really forgot to use it.
I actually created another user function where it will locate duplicate values and change the color.
But.. yours is the best one. Once again thanks a lot for your help.
Regards,
Ramana
initial thought.. why are you
initial thought.. why are you using VBA to colour cells, and not Conditional formatting ?
25. Excel Tips - Conditional formatting
Hi Nick, I haven't spend much
Hi Nick,
I haven't spend much time with excel.. i am basically from .Net
so, i was using loops rather than conditional format. But, as you said its really good use conditional format as it avoids unnecessary looping. I Current i am looking more using excel functionality rather using programming concepts. It really reduces the code.
Anyway thanks a lot for your comments.
Regards,
Ramana