[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

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
0
rinkydink
Asked:
rinkydink
  • 6
  • 3
1 Solution
 
jppintoCommented:
rRange.Value is not valid! You can't compare the Value of a cell with the value of a Range!
0
 
rinkydinkAuthor Commented:
Can you give a way to actually do this then?
0
 
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
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
jppintoCommented:
What are you trying to do here? Find duplicates inside a range?
0
 
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
0
 
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

0
 
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
0
 
rinkydinkAuthor Commented:
thank you!
0
 
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"! :)
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now