Compare two sheets with columns/rows inserted

bcc5025

New Member
Joined
Oct 23, 2015
Messages
3
Hi Everyone!

I've been working on this problem for a while and I still can't figure it out. Essentially I will be taking a snapshot of data, and then comparing it to new data months later to find out what changed. I need a VBA code to highlight these differences even when rows/columns are inserted.

Original Data:

AgeGenderHeight (in)Weight (lb)
Jim21M72182
Sally23F69120
Bob35M70175
Rachel31F65140
Mary27F66147
Greg27M69205

<colgroup><col span="5"></colgroup><tbody>
</tbody>



New Data (differences bolded):

AgeGenderShoe SizeHeight (in)Weight (lb)
Jim21M1072181
Sally23F769120
Bob35M9.570180
Brian29M970186
Rachel31F6.565140
Mary28F868147
Greg27M1169205

<colgroup><col span="6"></colgroup><tbody>
</tbody>

Anyone have suggestions? Index-match doesn't work because it won't recognize then rows/columns are inserted, so I guess I need some kind of two dimensional lookup? (Mary-Height was 66 now 68 = highlight)

Thanks!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
bcc5025,

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?

Here is a macro solution for you to consider.

The macro will first ask for the worksheet names (in this example, Original, and, New).

Both entered worksheet names will have to exist for the macro to continue.

Sample raw data in worksheet Original:


Excel 2007
ABCDEF
1NameAgeGenderHeight (in)Weight (lb)
2Jim21M72182
3Sally23F69120
4Bob35M70175
5Rachel31F65140
6Mary27F66147
7Greg27M69205
8
Original


Sample raw data in worksheet New:


Excel 2007
ABCDEFG
1NameAgeGenderShoe SizeHeight (in)Weight (lb)
2Jim21M1072181
3Sally23F769120
4Bob35M9.570180
5Brian29M970186
6Rachel31F6.565140
7Mary28F868147
8Greg27M1169205
9
New


And, after the macro:


Excel 2007
ABCDEFG
1NameAgeGenderShoe SizeHeight (in)Weight (lb)
2Jim21M1072181
3Sally23F769120
4Bob35M9.570180
5Brian29M970186
6Rachel31F6.565140
7Mary28F868147
8Greg27M1169205
9
New


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).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub CompareNewToOriginal()
' hiker95, 12/20/2015, ME909209
Dim woname As String, wnname As String
Dim wo As Worksheet, wn As Worksheet
Dim lro As Long, lco As Long, lrn As Long, lcn As Long
Dim r As Long, c As Long
Dim t As Range, n As Range
woname = InputBox("Enter the Original worksheet name")
If Not WorksheetExists(woname) Then
  MsgBox ("Worksheet '" & woname & "' DOES NOT EXIST - macro termnated!")
  Exit Sub
End If
wnname = InputBox("Enter the New worksheet name")
If Not WorksheetExists(wnname) Then
  MsgBox ("Worksheet '" & wnname & "' DOES NOT EXIST - macro termnated!")
  Exit Sub
End If
Application.ScreenUpdating = False
Set wo = Sheets(woname)
Set wn = Sheets(wnname)
With wn
  .Activate
  lrn = .Cells(Rows.Count, 1).End(xlUp).Row
  lcn = .Cells(1, Columns.Count).End(xlToLeft).Column
  'Bold new Title Rows
  For c = 2 To lcn
    Set t = wo.Rows(1).Find(.Cells(1, c).Value, LookAt:=xlWhole)
    If t Is Nothing Then
      .Range(.Cells(1, c), .Cells(lrn, c)).Font.Bold = True
      .Columns(c).AutoFit
    End If
  Next c
  'Check Names
  For r = 2 To lrn
    Set n = wo.Columns(1).Find(.Cells(r, 1).Value, LookAt:=xlWhole)
    If n Is Nothing Then
      .Range(.Cells(r, 1), .Cells(r, lcn)).Font.Bold = True
    ElseIf Not n Is Nothing Then
      For c = 2 To lcn
        If Not .Cells(r, 1).Font.Bold = True Then
          Set t = wo.Rows(1).Find(.Cells(1, c).Value, LookAt:=xlWhole)
          If Not t Is Nothing Then
            If .Cells(r, c).Value <> wo.Cells(n.Row, t.Column) Then
              .Cells(r, c).Font.Bold = True
            End If
          End If
        End If
      Next c
    End If
  Next r
  .Columns(1).Resize(, lcn).AutoFit
End With
Application.ScreenUpdating = True
End Sub
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the CompareNewToOriginal macro.
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

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