Finding the differences then copy and paste code to a new sheet

Nano

New Member
Joined
Jun 10, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I've been trying to write this VBA code to automate the process of finding differences between 2 original source sheets and then copying the entire row of the differences into a new sheet.

the process should be like this:
  • The code should look into Column A for both source sheets, finding only the right value to start the comparison, for example: InStr(cell1.Value, "1") > 0
  • after that, the code should look into column B for the actual differences. if the code finds a difference, then it would then copy the entire row where that different value is in. for example: the differences is in B2 in sheet A and B3 in sheet B, then it would copy the entire row 2 from sheet A and row 3 from sheet B to the new sheet
  • When the code paste to a new sheet, it should paste the rows from sheet A at from the first column, and rows from sheet B to 3 columns after the end columns of sheet A. for example: Since both source sheet has the same amount of columns, so when pasting sheet A to the new sheet, it should be from Column A to AJ, then pasting rows from sheet B should paste into the new sheet from Column AN to BW, leaving AJ, AK and AL free
Here is the code that I've tried to make so far. Do note that I have not add all feature i wanted because I've been getting errors from the result.

VBA Code:
Sub GetLevel1Difference()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim col1 As Range, col2 As Range
    Dim cell1 As Range, cell2 As Range
    Dim wsResult As Worksheet
    Dim lastRow1 As Long
    Dim lastRow2 As Long
    Dim i As Long
    Dim j As Long
    Dim resultRow As Long
    Dim maxCols As Long
    Dim rng1 As Range
    Dim rng2 As Range

    ' Set the worksheets
    Set ws1 = ThisWorkbook.Sheets("Sheet 1") ' Change "Sheet1" to your first sheet name
    Set ws2 = ThisWorkbook.Sheets("Sheet 2") ' Change "Sheet2" to your second sheet name
    
    Set col1 = ws1.Range("A:A")
    Set col2 = ws2.Range("A:A")
    
    
    ' Create a new worksheet for results
    Set wsResult = ThisWorkbook.Sheets("ComparisonResult") 

    ' Get the last used row in columns of both sheets
    lastRow1 = ws1.Cells(ws1.Rows.Count, col1.Column).End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, col2.Column).End(xlUp).Row
    
    ' Get the maximum columns used in ws1
    maxCols = ws1.UsedRange.Columns.Count + 3
    
    ' Initialize the result row counter
    resultRow = 2
    
    ' Loop through each row in the first sheet
    For i = 2 To lastRow1
        ' Find matching row in the second sheet
        For j = 2 To lastRow2
        
            Set cell1 = col1.Cells(i, 1)
            Set cell2 = col2.Cells(j, 1)
            
            ' Compare values in the specified column
            If cell1.Value = cell2.Value And InStr(cell1.Value, "1") > 0 And InStr(cell2.Value, "1") > 0 Then
            
                ' Define the range for the row to copy from ws1
                Set rng1 = ws1.Range(ws1.Cells(i, 1), ws1.Cells(i, ws1.UsedRange.Columns.Count))
                
                ' Define the range for the row to copy from ws2
                Set rng2 = ws2.Range(ws2.Cells(j, 1), ws2.Cells(j, ws2.UsedRange.Columns.Count))
                
                 ' Copy the entire row from the first sheet to the result sheet
                rng1.Copy
                wsResult.Cells(resultRow, 1).PasteSpecial Paste:=xlPasteValues

                ' Copy the entire row from the second sheet to the result sheet, offset by maxCols
                rng2.Copy
                wsResult.Cells(resultRow, maxCols).PasteSpecial Paste:=xlPasteValues
                
                ' Increment the result row counter
                resultRow = resultRow + 1
                Exit For
            End If
        Next j
    Next i
    
    ' Clear the clipboard
    Application.CutCopyMode = False
    
    MsgBox "Comparison and copy completed!", vbInformation

End Sub

The code does what it should do for the 1st sheet, finding the right value for Column A then pasting it to the new sheet at the right location.

However, For the second sheet, it copy the same values from the first result it found and then pasting it all repeatedly until it matched the amount of result from the first sheet. It should be noted that the 2 sheets have a different amount of rows.

Screenshot 2024-06-10 093737.png


Can anyone help with this?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I think we need more information to be able to help with this.
It's not clear to me what you mean by:
  • after that, the code should look into column B for the actual differences. if the code finds a difference, then it would then copy the entire row where that different value is in. for example: the differences is in B2 in sheet A and B3 in sheet B, then it would copy the entire row 2 from sheet A and row 3 from sheet B to the new sheet
In a typical pair of Excel columns generally every value in one column will differ from the value in every row of the other column except one. Do you mean that it should take the value in B2 and compare it against every other row in column B from the other sheet looking for differences, then repeat taking value in B3 and compare to every other and so on?

Also it's very hard to reproduce/fix/test issues without data. Can you paste a data sample using XL2BB or uploading a sample workbook to Onedrive or similar and sharing the link?
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,957
Members
452,539
Latest member
delvey

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