Hi
I'm using the below code to compare data in two columns in two workbooks and any cells from one column not included in the other are paste into the third workbook. I have 3 workbooks with 10 sheets in each workbook. One column from each sheet from first workbook is compared with matching column on matching sheet in second workbook and any missing entries are copied to the matching sheet on the third workbook .
The above works great on 9 sheets but the code on the 10th is not reporting missing cells but actually is copying the whole column from both workbooks into the third workbook.
The only difference is that this 10th sheet has 55k rows where others sheet have no more than 19k rows.
The data is supplied by 10 suppliers so I thought that this might me something to do with the excel files as these reports are generated by systems that create the xlsx files as an output file and maybe this has something to do with CHAR(160) / CHAR(32) limitation so I used this formula =TRIM(CLEAN(SUBSTITUTE(E1,CHAR(160)," "))) before I run the code. The result is the same.
I also used another code as an alternative:
The above codes gives me an error: Runtime error 1004. Application-defined or object-defined error. However some of the code was executed and the result is what I expected but not all lines have been compared as the code returns an error. This usually stops around the row 3k on the third workbook (result workbook).
I have spent so much time now to find the solution but nothing seems to be working.
Can someone please check and advise how this can be sorted?
Thanks
I'm using the below code to compare data in two columns in two workbooks and any cells from one column not included in the other are paste into the third workbook. I have 3 workbooks with 10 sheets in each workbook. One column from each sheet from first workbook is compared with matching column on matching sheet in second workbook and any missing entries are copied to the matching sheet on the third workbook .
Code:
Sub Compare()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, lr1 As Long, lr2 As Long, rng1 As Range, rng2 As Range, C As Range
Set wbk1 = Workbooks.Open("P:\Transfer.xlsm")
Set wbk2 = Workbooks.Open("P:\Golden Source.xlsm")
Set sh1 = wbk1.Worksheets("ABC")
Set sh2 = wbk2.Worksheets("ABC")
Set sh3 = Workbooks("Comparision.xlsm").Worksheets("ABC")
lr1 = sh1.Cells(Rows.Count, 1).End(xlUp).Row 'Get the last row with data for both list sheets
lr2 = sh2.Cells(Rows.Count, 1).End(xlUp).Row
Set rng1 = sh1.Range("N2:N" & lr1) 'Establish the ranges on both sheets
Set rng2 = sh2.Range("N2:N" & lr2)
For Each C In rng1 'Run a loop for each list ID mismatches and paste to sheet 3.
If WorksheetFunction.CountIf(rng2, C.Value) = 0 Then
sh3.Cells(Rows.Count, 1).End(xlUp)(2) = C.Value
End If
Next
For Each C In rng2
If Application.CountIf(rng1, C.Value) = 0 Then
sh3.Cells(Rows.Count, 2).End(xlUp)(2) = C.Value
End If
Next
Windows("Transfer.xlsm").Close
Windows("Golden Source.xlsm").Close
MsgBox "Done"
End Sub
The above works great on 9 sheets but the code on the 10th is not reporting missing cells but actually is copying the whole column from both workbooks into the third workbook.
The only difference is that this 10th sheet has 55k rows where others sheet have no more than 19k rows.
The data is supplied by 10 suppliers so I thought that this might me something to do with the excel files as these reports are generated by systems that create the xlsx files as an output file and maybe this has something to do with CHAR(160) / CHAR(32) limitation so I used this formula =TRIM(CLEAN(SUBSTITUTE(E1,CHAR(160)," "))) before I run the code. The result is the same.
I also used another code as an alternative:
Code:
Sub Compare()
'UpdatebyExtendoffice 20160623
Dim I As Long, J As Long, K As Long, M As Long
Application.ScreenUpdating = False
J = 1
K = 1
M = Cells(Rows.Count, 1).End(xlUp).Row
Z = Cells(Rows.Count, 2).End(xlUp).Row
Range("D1").Value = "A not in B"
Range("E1").Value = "B not in A"
For I = 2 To M
If IsError(Application.match(Range("A" & I).Value, Columns("B"), 0)) Then
J = J + 1
Range("D" & J).Value = Range("A" & I).Value
End If
Next I
For I = 2 To Z
If IsError(Application.match(Range("B" & I).Value, Columns("A"), 0)) Then
K = K + 1
Range("E" & K).Value = Range("B" & I).Value
End If
Next I
Application.ScreenUpdating = True
End Sub
The above codes gives me an error: Runtime error 1004. Application-defined or object-defined error. However some of the code was executed and the result is what I expected but not all lines have been compared as the code returns an error. This usually stops around the row 3k on the third workbook (result workbook).
I have spent so much time now to find the solution but nothing seems to be working.
Can someone please check and advise how this can be sorted?
Thanks