Excel VBA question - type mismatch error

Hi I am trying to simply loop through a range of cells and find any matching values but I get a type mismatch error.

Any help is appreciated!

Thanks

below is my code

Dim rRange As Range

Dim rCell As Range



    Set rRange = Range("b1:g1")

        For Each rCell In rRange

        If rCell.Value = rRange.Value Then rCell.Font = vbRed
               

    Next rCell

End Sub
LVL 1
rinkydinkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jppintoCommented:
rRange.Value is not valid! You can't compare the Value of a cell with the value of a Range!
rinkydinkAuthor Commented:
Can you give a way to actually do this then?
jppintoCommented:
You could compare a Range.Value if the range was only a cell like this:

If Range("A1").Value=Cell("A1").Value

But the rRange is a multiple cells range.

jppinto
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

jppintoCommented:
What are you trying to do here? Find duplicates inside a range?
jppintoCommented:
I believe that you're trying to find duplicates on a range of cells and change the color of the font on those duplicated values to Red. Is the code and a sample file on how to do it.

jppinto
Sub HighlightDuplicates()
Dim rRange As Range
Dim rCell As Range
Set rRange = Range("B1:G1")
    For Each rCell In rRange
        If Application.WorksheetFunction.CountIf(Range("B1:G1"), rCell.Text) > 1 Then
            rCell.Font.Color = vbRed
        End If
    Next rCell
End Sub

Open in new window

HighlightDuplicatesRange.xlsm
rinkydinkAuthor Commented:
Thanks for the code, I have 2 ranges however and what I am trying to do is locate any matches in the second range and turn that cell font red on the matching value, sorry i probably didn't explain myself well enough.

i'm trying to tweak the code you provided to see if i can figure it out but any suggestions are welcome for sure

jppintoCommented:
Yes, the question was not properly explained! That's a completly different problem!

Try this. This will change the font color to Red on the first range when it finds the value on the second range.

jppinto
Sub HighlightDuplicates()
Dim rRange1, rRange2 As Range
Dim rCell1 As Range
Set rRange1 = Range("B1:G1")
Set rRange2 = Range("B2:G2")
    For Each rCell1 In rRange1
        If Application.WorksheetFunction.CountIf(rRange2, rCell1.Text) > 0 Then
            rCell1.Font.Color = vbRed
        End If
    Next rCell1
End Sub

Open in new window

HighlightDuplicatesRange.xlsm

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rinkydinkAuthor Commented:
thank you!
jppintoCommented:
You're welcome! Next time, I suggest that you make a better explanation of what you want so that we don't spend time "chasing the wrong rabbit"! :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.