VBA Match data, find mismatch data and copy data to validation sheet. Not working correctly.

pawcoyote

Board Regular
Joined
Mar 5, 2012
Messages
89
Office Version
  1. 365
Platform
  1. Windows
Hi, I am trying to do the following with the example worksheet. I see that we cannot upload workbooks anymore. I tried the L2BB but that doesn't work for some reason.

I want to Index and Match the Serial Numbers on Site1 and Site2, then I want to look for mismatched data in the following columns on Site1 and Site2. If it finds a mismatch it copies only the data from Site1 and Site2 which will show the mismatch data. I am also looking for a way to state on the Validation sheet in the Evaluation column what is mismatched. That's a work in progress.

Site1: Yellow is mismatch data so we can see it better. Color coded the headers with information that should be copied to Validation if there is a mismatch.
site1_image.jpg


Site2: Yellow is mismatch data so we can see it better. Color coded the header with information that should be copied to Validation if there is a mismatch.
site2_image.jpg


Validation:
validation_image.jpg


Below is the code I used. It copies everything over not just the mismatch data.
VBA Code:
Sub Validation()
Dim wsSite1 As Worksheet
Dim wsSite2 As Worksheet
Dim wsValidation As Worksheet
Dim lastRowSite1 As Long
Dim lastRowSite2 As Long
Dim validationRow As Long
Dim i As Long

' Set references to worksheets
Set wsSite1 = ThisWorkbook.Worksheets("Site1")
Set wsSite2 = ThisWorkbook.Worksheets("Site2")
Set wsValidation = ThisWorkbook.Worksheets("Validation")

' Find the last row in each worksheet
lastRowSite1 = wsSite1.Cells(wsSite1.Rows.Count, "A").End(xlUp).Row
lastRowSite2 = wsSite2.Cells(wsSite2.Rows.Count, "A").End(xlUp).Row

' Initialize the row number for the Validation sheet
validationRow = 2 ' Start from row 2 to leave the header row intact

' Loop through each row in Site1
For i = 2 To lastRowSite1 ' Assuming data starts from row 2 and has headers

' Get the serial number from Site1
serialNumberSite1 = wsSite1.Cells(i, "A").Value

' Search for a matching serial number in Site2
matchingRowSite2 = Application.Match(serialNumberSite1, wsSite2.Range("A2:A" & lastRowSite2), 0)

If Not IsError(matchingRowSite2) Then
' If a match is found, compare the data
If wsSite1.Cells(i, "G").Value <> wsSite2.Cells(matchingRowSite2, "E").Value Or _
wsSite1.Cells(i, "H").Value <> wsSite2.Cells(matchingRowSite2, "F").Value Or _
wsSite1.Cells(i, "I").Value <> wsSite2.Cells(matchingRowSite2, "G").Value Or _
wsSite1.Cells(i, "J").Value <> wsSite2.Cells(matchingRowSite2, "H").Value Then

' **Copy the mismatched data to the Validation sheet**
wsSite1.Range("D" & i & ":J" & i).Copy wsValidation.Cells(validationRow, "B")
wsSite2.Range("B" & i & ":H" & i).Copy wsValidation.Cells(validationRow, "I")

validationRow = validationRow + 1 ' Move to the next row in the Validation sheet
End If
End If
Next i

' Clean up
Set wsSite1 = Nothing
Set wsSite2 = Nothing
Set wsValidation = Nothing
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
matchingRowSite2 is calculated relative to Row 2, but then you use the result as if it were an absolute value

Try using
Rich (BB code):
matchingRowSite2 = Application.Match(serialNumberSite1, wsSite2.Range("A1:A" & lastRowSite2), 0)
(note A1 instead than your A2)
 
Upvote 1
Solution
And why your vba code thinks that SerialNumber is in column A whereas the images suggest that they are in column F (site1) and column D (site2)??
 
Upvote 1
And why your vba code thinks that SerialNumber is in column A whereas the images suggest that they are in column F (site1) and column D (site2)??
I just noticed that I cleaned up the example sheet from my master that I have. Trying to provide a smaller snippet of what I have instead of the other stuff...
 
Upvote 0
Hi, made that simple change, just didn't see it. It is bringing in some of the mismatches and still some of the items that match. See below. I am also looking at a way to identify the items that haven an issue and noting that in the Evaluation column on the Validation sheet. If you have a suggest that would be grand.

Validation: It is bringing in two (2) items that do not have any issues on them (with Red arrows). It is also missing one (1) mismatch/error from Site2 (See Site2 Image).
validation2.jpg


Site2: The line with the error in the Postal Code is not being found.
Site2_2.jpg


VBA:
VBA Code:
Sub Validation()
Dim wsSite1 As Worksheet
Dim wsSite2 As Worksheet
Dim wsValidation As Worksheet
Dim lastRowSite1 As Long
Dim lastRowSite2 As Long
Dim validationRow As Long
Dim i As Long

' Set references to worksheets
Set wsSite1 = ThisWorkbook.Worksheets("Site1")
Set wsSite2 = ThisWorkbook.Worksheets("Site2")
Set wsValidation = ThisWorkbook.Worksheets("Validation")

' Find the last row in each worksheet
lastRowSite1 = wsSite1.Cells(wsSite1.Rows.Count, "A").End(xlUp).Row
lastRowSite2 = wsSite2.Cells(wsSite2.Rows.Count, "A").End(xlUp).Row

' Initialize the row number for the Validation sheet
validationRow = 2 ' Start from row 2 to leave the header row intact

' Loop through each row in Site1
For i = 2 To lastRowSite1 ' Assuming data starts from row 2 and has headers

' Get the serial number from Site1
serialNumberSite1 = wsSite1.Cells(i, "F").Value

' Search for a matching serial number in Site2
matchingRowSite2 = Application.Match(serialNumberSite1, wsSite2.Range("D2:D" & lastRowSite2), 0)

If Not IsError(matchingRowSite2) Then
' If a match is found, compare the data
If wsSite1.Cells(i, "G").Value <> wsSite2.Cells(matchingRowSite2, "E").Value Or _
wsSite1.Cells(i, "H").Value <> wsSite2.Cells(matchingRowSite2, "F").Value Or _
wsSite1.Cells(i, "I").Value <> wsSite2.Cells(matchingRowSite2, "G").Value Or _
wsSite1.Cells(i, "J").Value <> wsSite2.Cells(matchingRowSite2, "H").Value Then

' **Copy the mismatched data to the Validation sheet**
wsSite1.Range("D" & i & ":J" & i).Copy wsValidation.Cells(validationRow, "B")
wsSite2.Range("B" & i & ":H" & i).Copy wsValidation.Cells(validationRow, "I")

validationRow = validationRow + 1 ' Move to the next row in the Validation sheet
End If
End If
Next i

' Clean up
Set wsSite1 = Nothing
Set wsSite2 = Nothing
Set wsValidation = Nothing
End Sub
 
Upvote 0
VBA Code:
wsSite2.Range("D2:D" & lastRowSite2)
This is wrong, as I told you in my first message; it should be "D1:D" & etc
 
Upvote 0
This is wrong, as I told you in my first message; it should be "D1:D" & etc
Thank you that fixed it. I am learning as I go. I can mark this as complete and thank you for your help. Going to work on the Evaluation portion now. Want to be able to have either note what is a mismatch or possibly highlight the mismatch data on the Validation sheet. You have been very helpful.
 
Upvote 0
This is wrong, as I told you in my first message; it should be "D1:D" & etc
Hi Anlthony47, have a follow-up question. What if my data on my Site1 and Site2 is not in a logical Order like above. If they are spread across the spreadsheets. I am not seeing how I can put that into the following area for the copy of data.. I am now trying to see how I can use this code on a much larger and expansive workbook. As you can see the Column numbers are not in order and do not flow in order. I am seeing I cannot use a Range to Copy the Mismatch or at least not only a Range. Any guidance is much appreciated.

Site1 looks like this.
OList.jpg



Site2 Looks like this.
MList.jpg



VBA Code:
                ' Copy the mismatched data to the Validation sheet
                wsSite1.Range("D" & i & ":J" & i).Copy wsValidation.Cells(validationRow, "B")
                wsSite2.Range("B" & matchingRowSite2 & ":H" & matchingRowSite2).Copy wsValidation.Cells(validationRow, "J")
 
Upvote 0
Hummm... In the initial situation we used the "Serial Number" to lookup the information and compare them.
Which field(s) would be used in this new scenario to match info from Site1 and those from Site2?
 
Upvote 0
Hummm... In the initial situation we used the "Serial Number" to lookup the information and compare them.
Which field(s) would be used in this new scenario to match info from Site1 and those from Site2?
Hi, it would still be the serial number that is the primary key and match. Site1 Column B and Site2 Column P are the serial matches.

Below is the full code. I was able to add a clear all on the Validation sheet starting in A2. Was also able to add a mismatch into Column A on the Validation. Now I need to figure out how to copy the data in the different cells that mismatch. It was much easier with the data in order.

VBA Code:
Sub Validation()
    Dim wsSite1 As Worksheet
    Dim wsSite2 As Worksheet
    Dim wsValidation As Worksheet
    Dim serialNumberSite1 As Variant    ' Initialize the Serial Number in the worksheet
    Dim matchingRowSite2 As Variant     ' Initialize the Serial Number in the worksheet
    Dim lastRowSite1 As Long
    Dim lastRowSite2 As Long
    Dim validationRow As Long
    Dim i As Long

    ' Set references to worksheets
    Set wsSite1 = ThisWorkbook.Worksheets("Site1")
    Set wsSite2 = ThisWorkbook.Worksheets("Site2")
    Set wsValidation = ThisWorkbook.Worksheets("Validation")

    ' Clear all cells in the Validation sheet starting from A2 (excluding the header row)
    wsValidation.Range("A2").Resize(wsValidation.Rows.Count - 1, wsValidation.Columns.Count).ClearContents

    ' Find the last row in each worksheet
    lastRowSite1 = wsSite1.Cells(wsSite1.Rows.Count, "A").End(xlUp).Row
    lastRowSite2 = wsSite2.Cells(wsSite2.Rows.Count, "A").End(xlUp).Row

    ' Initialize the row number for the Validation sheet
    validationRow = 2 ' Start from row 2 to leave the header row intact

    ' Loop through each row in Site1
    For i = 2 To lastRowSite1 ' Assuming data starts from row 2 and has headers

        ' Get the serial number from Site1
        serialNumberSite1 = wsSite1.Cells(i, "B").Value

        ' Search for a matching serial number in Site2
        matchingRowSite2 = Application.Match(serialNumberSite1, wsSite2.Range("P1:P" & lastRowSite2), 0)

        If Not IsError(matchingRowSite2) Then
            ' If a match is found, compare the data
            If wsSite1.Cells(i, "G").Value <> wsSite2.Cells(matchingRowSite2, "E").Value Or _
               wsSite1.Cells(i, "H").Value <> wsSite2.Cells(matchingRowSite2, "F").Value Or _
               wsSite1.Cells(i, "I").Value <> wsSite2.Cells(matchingRowSite2, "G").Value Or _
               wsSite1.Cells(i, "J").Value <> wsSite2.Cells(matchingRowSite2, "H").Value Then

[B][COLOR=rgb(65, 168, 95)]                ' Copy the mismatched data to the Validation sheet[/COLOR][/B]
[COLOR=rgb(65, 168, 95)][B]                wsSite1.Range("D" & i & ":J" & i).Copy wsValidation.Cells(validationRow, "B")[/B][/COLOR]
[B][COLOR=rgb(65, 168, 95)]                wsSite2.Range("B" & matchingRowSite2 & ":H" & matchingRowSite2).Copy wsValidation.Cells(validationRow, "J")[/COLOR][/B]

                ' Insert the mismatch statement in cell A of the Validation sheet
                wsValidation.Cells(validationRow, "A").Value = "Mismatch Found"

                validationRow = validationRow + 1 ' Move to the next row in the Validation sheet
            End If
        End If

    Next i

    ' Clean up
    Set wsSite1 = Nothing
    Set wsSite2 = Nothing
    Set wsValidation = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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