Type Mismatch Error When Comparing Ranges

phenley3

New Member
Joined
Jun 1, 2018
Messages
4
I have the following code and get a type mismatch error when comparing two ranges through a loop. When comparing the rows, the first column has a number and the other three contain strings. The idea is to find any differences between the two sheets and put them into another sheet. I changed .Value to .Text in the if statement, and it ran, but it put everything into the other sheet as though everything was different.

Code:
Set target = output.Range("B3:D3")
For i = 13 To prevws.Cells(13, 2).End(xlDown).Row
    Set test = prevws.Range(prevws.Cells(i, 2), prevws.Cells(i, 5))
    For w = 13 To curws.Cells(13, 2).End(xlDown).Row
        Set comp = curws.Range(curws.Cells(w, 2), curws.Cells(w, 5))
[B]        If test.Value = comp.Value Then[/B]
            same = True
            Exit For
        Else
            same = False
        End If
    Next w
    If same = False Then
            target.Value = test.Value
            Set target = target.Offset(1, 0)
    End If
Next i
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the forum.

You can't compare one array to another using the = operator. You'd have to loop through each array.
 
Last edited:
Upvote 0
Thanks for the reply.

So I'd have to add a third loop within the second that loops through each value in the range?
 
Upvote 0
Either that or do something like joining the arrays into delimited strings that you can directly compare.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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