compare data between seperate data ranges

CYDSRQ

New Member
Joined
Nov 26, 2013
Messages
35
Hello - I am relatively new to analyzing data in Excel - using Windows 7 and Excel 2010. I have two sets of data and am trying to validate data between them. Data sets have columns titled FirstName, LastName, Ethnicity, Gender. I am trying to compare the Ethnicity and Gender to determine that the data set2 info is correct (matches data set 1). I am familiar with Index: Match, but don't think that applies here? Thanks for any help on this! Karen
 
CYDSRQ,

Just to clarify - the macro highlighted cells when data in Data Set 2 was not the same as Data Set 1? That's what it appears to be....

I am not sure what is possible- can the cell in data set 2 be updated with correct info from set 1, and highlighted in yellow to show that it has been updated? If not, then highlighting the incorrect information in data set 2 would be helpful.

I missed this part - be back in a little while.


Can you suggest ways to learn about building macros, and/or VBA ?

See the below link for my most up to date list:

Training / Books / Sites as of 1/30/2014
http://www.mrexcel.com/forum/excel-...umber-statement-end-statment.html#post3706277
 
Last edited:
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
CYDSRQ,

Sample raw data worksheets:


Excel 2007
ABCDE
1firstlaststudentidgenderethnicity
2janedoe123456fw
3mikewilliams234567mb
4joselobaton345678mh
5tomsmith456789mw
6
Data Set 1



Excel 2007
ABCD
1firstlastgenderethnicity
2sandyoatesfw
3janedoeh
4mikewilliamsmw
5
Data Set 2


After the new macro in worksheet Data Set 2:


Excel 2007
ABCD
1firstlastgenderethnicity
2sandyoatesfw
3janedoefw
4mikewilliamsmb
5
Data Set 2


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub CompareDatasetsV2()
' hiker95, 02/25/2014, ME759423
Dim lr1 As Long, lr2 As Long
Dim nrng As Range, c As Range
Application.ScreenUpdating = False
With Sheets("Data Set 1")
  lr1 = .Cells(Rows.Count, 1).End(xlUp).Row
  With Range("A1:E" & lr1)
    .Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
  End With
  With .Range("F2:F" & lr1)
    .FormulaR1C1 = "=RC[-5]&RC[-4]"
    .Value = .Value
  End With
End With
With Sheets("Data Set 2")
  lr2 = .Cells(Rows.Count, 1).End(xlUp).Row
  With Range("A1:D" & lr2)
    .Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
  End With
  With .Range("E2:E" & lr1)
    .FormulaR1C1 = "=RC[-4]&RC[-3]"
    .Value = .Value
  End With
End With
With Sheets("Data Set 2")
  For Each c In .Range("E2:E" & lr2)
    Set nrng = Sheets("Data Set 1").Range("F2:F" & lr1).Find(c, LookAt:=xlWhole)
    If Not nrng Is Nothing Then
      If .Cells(c.Row, 3) <> Sheets("Data Set 1").Cells(nrng.Row, 4) Then
        .Cells(c.Row, 3) = Sheets("Data Set 1").Cells(nrng.Row, 4)
        .Cells(c.Row, 3).Interior.Color = 65535
      End If
      If .Cells(c.Row, 4) <> Sheets("Data Set 1").Cells(nrng.Row, 5) Then
        .Cells(c.Row, 4) = Sheets("Data Set 1").Cells(nrng.Row, 5)
        .Cells(c.Row, 4).Interior.Color = 65535
      End If
    End If
  Next c
End With
Sheets("Data Set 1").Range("F2:F" & lr1).ClearContents
Sheets("Data Set 2").Range("E2:E" & lr2).ClearContents
With Sheets("Data Set 2")
  .Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CompareDatasetsV2 macro.
 
Upvote 0
Thank you hiker95! That worked very well! I also appreciate the link to the VBA info....
 
Upvote 0
CYDSRQ,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,317
Members
449,218
Latest member
Excel Master

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