Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

compare two sheets and show different values

0

hello

I  try  making  macro   to  compare  two  sheets  based on  COL  A    and   if  the  items  existed  in  sheet1  and  not  existed  in  sheet2  should  add  to  sheet2  as  highlighted  by  red  when  matching  two  sheets  should  show  deficit  or  surpluses  the  values  the  subtracting  should  do    sheet2  from  sheet1    then  tick  right or  wrong  by  x  and  if   the  values =0  then  tick right  I put  expected  result  IN  COLS C,D,E in  sheet2 

Sub UpdateSheet2()
Dim LR As Long

With Sheets("Sheet2")
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    With .Range("d2:d" & LR)
        .Formula = "=IF(ISNUMBER(MATCH(A2, Sheet1!A:A, 0)), B2 - VLOOKUP(A2, Sheet1!A:B, 2, 0), B2)"
        .Value = .Value

    End With

End With

End Sub

Answer
Discuss

Answers

0
Selected Answer

Hi Leap

In the attached version of your workbook, I've expanded your code to that below (with comments).

Firstly I loop through Sheet1 to detect the rows not recorded in Sheet2 and add those to the end of sheet2. I set those additions to a red fill (using the line ending ...Interior.Color = vbRed but you can delete that if not needed). Note that I had some problems with your file so used Sheets(Sheets1") etc. a lot.

After that, your comparison code is run then I adjust columns D:F using a Select Case/ End Select (instead of mutilpe If statements) to add ticks and crosses and to move positive values to the surplus column:

Sub UpdateSheet2()

Dim LR1 As Long, LR2 As Long, n As Long

LR1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
LR2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

' First find (and add) any Brands which do not exist on Sheet 2
For Each Cll In Sheets("Sheet1").Range("A2:A" & LR1)
    If Not IsNumeric(Application.Match(Cll.Value, Sheets("Sheet2").Columns(1), 0)) Then 'if no Exact Match...
        LR2 = LR2 + 1
        Cll.Resize(1, 2).Copy Sheets("Sheet2").Range("A" & LR2)
        Sheets("Sheet2").Range("B" & LR2).Value = "" 'set to blank
        Sheets("Sheet2").Range("A" & LR2).Resize(1, 5).Interior.Color = vbRed
    End If
Next Cll

' Compare the values for Sheet1 and Sheet 2
With Sheets("Sheet2")

    With .Range("D2:D" & LR2)
        .Formula = "=IF(ISNUMBER(MATCH(A2, Sheet1!A:A, 0)), B2 - VLOOKUP(A2, Sheet1!A:B, 2, 0), B2)"
        .Value = .Value
    End With

    'set condition font (to include new rows)
    .Range("C2:C" & LR2).Font.Name = "Wingdings"
    ' adjust columns D:E
    For n = 2 To LR2
        Select Case .Range("D" & n)
            Case 0
                .Range("C" & n).Value = "ü" ' tick if no difference
            Case Is < 0
                .Range("C" & n).Value = "û" 'cross if different
            Case Is > 0
                .Range("C" & n).Value = "û"  'cross if different
                .Range("E" & n).Value = .Range("D" & n).Value 'move value
                .Range("D" & n).Value = ""
        End Select
    Next n

End With

End Sub

Hope this is what you need.

Discuss

Discussion

Hi John,
this  is  exactly  what  I'm looking  for     many thanks for  your  solution and additional   the  comments  inside  the  code   , indeed  rarely somebody  shows  how  the  code  works  ,  mostly  just  answer  you  and  give  the  code  without  show  how  the  code   works .
Best Regards,
Leap
leap (rep: 46) Apr 26, '21 at 6:24 am
Leap.

Thanks for selecting my Answer.

It occurred to me just (out on a walk) that you might want to add this additional line (in bold within an extract):
Cll.Resize(1, 2).Copy Sheets("Sheet2").Range("A" & LR2)

Sheets("Sheet2").Range("B" & LR2).Value="" 'set new quantity to blank
This will give the condition "x" in any new rows (like your example sheet had).
John_Ru (rep: 6142) Apr 26, '21 at 7:32 am
John
you're absolutely  right  yes  it  should  the  values  in  deficit  and tick  by  mark  x  I  no  know  how did I miss this error  , this  is  important  point 
thanks  again 
leap (rep: 46) Apr 26, '21 at 7:50 am
Leap

I've now added that (in my Answer and the macro/revised file). Also I corrected the code to declare both LR1 and LR2. BTW I'd already corrected your spelling of "surplus" in Sheet 2 column E.
John_Ru (rep: 6142) Apr 26, '21 at 8:20 am
John
thanks  again 
leap (rep: 46) Apr 26, '21 at 12:14 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login