Compare multiple columns in 2 workbook's different worksheet, then copy and past values

kp2016

New Member
Joined
May 19, 2016
Messages
2
Hi,

I am facing problem in my code, need expert help.

Let me first explain problem statement and also share code as well.

Problem Statement:
I have 2 different workbook where I have data to be compared in multiple columns, if match is found then copy rows and paste it in active workbook.

When I run the VBA code, I will keep both the workbooks open, since I know which are the columns to be compared I have specifically mentioned the column name.

Here is my code:

VBA Code:
Sub CompareColCopyRow()

    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim i As Long, j As Long
    Dim lastRow1 As Long, lastRow2 As Long
    Dim compareColumns1 As String, compareColumns2 As String
    Dim compareArray1 As Variant, compareArray2 As Variant
    Dim matchFound As Boolean
    
    Set wb1 = ActiveWorkbook 'the first workbook
    Set wb2 = Application.Workbooks("SOP Template.xlsx") 'the second workbook

    Set ws1 = wb1.Sheets("SPO_Mar") 'sheet name where data to be pasted
    Set ws2 = wb2.Sheets("CORTIX+Services+EMS") 'sheet name where data to be compared and copied
    
    lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row 'last row in the first sheet
    lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row 'last row in the second sheet
    
    compareColumns1 = "D,E,F,K" 'columns to be compared in the first file
    compareColumns2 = "F,B,H,M" 'columns to be compared in the second file
    
    compareArray1 = Split(compareColumns1, ",")
    compareArray2 = Split(compareColumns2, ",")
    
    For i = 1 To lastRow2 'loop through each row in the second sheet
        matchFound = False 'assume the row doesn't match
        For j = 1 To lastRow1 'loop through each row in the first sheet
            If ws2.Cells(i, compareArray2(0)).Value = ws1.Cells(j, compareArray1(0)).Value And _
               ws2.Cells(i, compareArray2(1)).Value = ws1.Cells(j, compareArray1(1)).Value And _
               ws2.Cells(i, compareArray2(2)).Value = ws1.Cells(j, compareArray1(2)).Value And _
               ws2.Cells(i, compareArray2(3)).Value = ws1.Cells(j, compareArray1(3)).Value Then 'if all the values in the specified columns match
                matchFound = True 'mark the row as matching
                Dim copyRange As Range
                Set copyRange = ws2.Range("AJ" & i & ":AU" & i)
                copyRange.Copy 'copy the content including formula and format from second file's AN to HV column
                ws1.Range("AJ" & j).PasteSpecial xlPasteAllUsingSourceTheme 'paste the content and format to first file's AN to HV column upon match
                ' copy column width and formatting
                copyRange.Copy
                ws1.Range("AJ" & j & ":AU" & j).PasteSpecial xlPasteColumnWidths
                ws1.Range("AJ" & j & ":AU" & j).PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
                Exit For 'exit the loop if a match is found
                End If
        Next j
    Next i
    
    ' copy colums which contains formulas
    ws2.Range("AU:HV").Copy
    ws1.Range("AU:HV").PasteSpecial xlPasteAllUsingSourceTheme
    ws1.Range("AU:HV").PasteSpecial xlPasteColumnWidths
    ws1.Range("AU:HV").PasteSpecial xlPasteFormats
    
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The code seems pretty sound. It should go through all the rows. But maybe there are small differences like a trailing space that causes issues.
You could set a watch (bottom right window in the VBA editor) so the code pauses when it reaches a row where you think it should copy, but it doesn't.

If you have a great number of rows, then this code will take a considerable time to run.
It could be made a lot more efficient by working with arrays rather than reading all these cells one by one and writing a line for each match.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,877
Messages
6,175,139
Members
452,615
Latest member
bogeys2birdies

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