comparing two excelsheets for uncommon records based on common id field (column)

Avidan

New Member
Joined
Mar 19, 2015
Messages
1
I am comparing two excelsheets in the same workbook.


I want to check whether the records from sheet1 are exactly same as records in sheet2 based on common Question_id(Column A of both worksheets)


This question_id(column A) has values such as



1
1a
1a.1
1a.1a
1a.1b
1a.1c
2
2a
2a.1
2a.1a
2a.1b
2a.1c etc....


I want to compare the records based on this Question_id(Column A Value).


If Question_id is same and records(the remaining row) are not same then am coloring those records in red background(only specific cells and not the whole row)


For the same, I have following code.


Sub RunCompare()

Call compareSheets("Sheet1", "Sheet2")

End Sub


Sub compareSheets(shtSheet1 As String, shtSheet2 As String)

Dim mycell As Range
Dim mydiffs As Integer

Application.ScreenUpdating = false

'Color Uncommon records in Red Background
For Each mycell In ActiveWorkbook.Worksheets(shtSheet1).UsedRange
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet2).Cells(mycell.Row, mycell.Column).Value Then

mycell.Interior.Color = vbRed
mydiffs = mydiffs + 1

End If
Next

'Display no. of differences
MsgBox mydiffs & " differences found", vbInformation

ActiveWorkbook.Sheets(shtSheet1).Select


MsgBox "Data Scrubbed Successfully..."
Application.ScreenUpdating = True
End Sub


The above code runs fine when I have same sequence of Question_id (and therefore of records) in both the excelsheets.


Assume I have different sequence of Question_id (and therefore of records) in both the sheets.


Then how I can achieve this...?


Something Like using where clause in my code `Where Sheet1.Question_id = Sheet2.Question_id`


i.e. I'll pick up question_id and the full row from sheet1 and I will compare it against records in sheet2 based on matching Question_id(value of Column A) only.




Can someone tell where I can put the condition and what type of condition so that, even if both the excelsheets have random sequences of Question_id; I will be able to compare the records from sheet1 and sheet2.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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