+ Reply to Thread
Results 1 to 16 of 16

vba compare between two sheets and highlight new items

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    vba compare between two sheets and highlight new items

    Hi experts ,
    I put the result in column column D,E for sheet2 and highlighted new item for sheet1,2
    so current code compares data between two sheets based on column B , subtraction numeric values between two sheets and result show in column E for sheet2 , if the values are positive or negative then will be in column D wrong mark for adjacent cell and if the value is zero or empty in column E will be in column D right mark and if there new item is existed in sheet but it's not in the other will highlight the item by red . if the item is in existed in sheet1 , but it's not existed in sheet2 the value in column E is negative, if the item is in existed in sheet2 but not in sheet1 the value in column E is positive
    so I want replace formula in column D with vba procedure and highlight new items between two sheets .
    should copy highlighted data from sheet1 to sheet2 to become as I put in rang I: M but the result should be in range A:E



    Sub UpdateSheet2()
    Dim LR As Long
    
    With Sheets("Sheet2")
        LR = .Range("B" & .Rows.Count).End(xlUp).Row
        With .Range("E2:E" & LR)
            .Formula = "=IF(ISNUMBER(MATCH(B2, Sheet1!B:B, 0)), C2 - VLOOKUP(B2, Sheet1!B:C, 2, 0), C2)"
            
            .Value = .Value
    
          
        End With
        
    End With
    
    End Sub
    thanks
    Attached Files Attached Files
    Last edited by leap out; 03-09-2023 at 04:29 AM.

  2. #2
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: replace formula with vba by compare between two sheets and highlight new items

    could be help guys?

  3. #3
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    no answering so far !
    posted here
    https://www.mrexcel.com/board/thread...items.1231959/

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: vba compare between two sheets and highlight new items

    Give this code a try:

    Sub UpdateSheet2q()
    Dim LR As Long, c As Range, k&, i&, j&, nx&, x&, r&
    Dim s1 As Worksheet, s2 As Worksheet
    Dim d As Object, dd As Object, a, b
    
    Set s1 = Sheets("sheet1")
    Set s2 = Sheets("sheet2")
    
    a = s1.[A1].Offset(1).CurrentRegion
    b = s2.[A1].Offset(1).CurrentRegion
    Set d = CreateObject("Scripting.Dictionary")
    Set dd = CreateObject("Scripting.Dictionary")
    d.CompareMode = vbTextCompare
        For i = LBound(a) To UBound(a)
            If Not d.exists(a(i, 2)) Then
                d.Add a(i, 2), k
                k = k + 1
            End If
        Next i
    For i = LBound(a) To UBound(a)
        For x = 2 To s2.Range("B" & s2.Rows.Count).End(xlUp).Row ' d.exists(s2.Cells(x, 2).Value) And
            If Not dd.exists(s2.Cells(x, 2).Value) Then
                dd.Add s2.Cells(x, 2).Value, r
                r = r + 1
            End If
            If s2.Cells(x, 2).Value = a(i, 2) Then
                s2.Cells(x, 5).Value = s2.Cells(x, 3).Value - a(i, 3)
                If s2.Cells(x, 5).Value = 0 Then
                    s2.Cells(x, 4).Value = Chr(252)
                Else
                    s2.Cells(x, 4).Value = Chr(251)
                End If
            x = x + 1
            End If
        Next
    Next
        nx = s2.Range("B" & s2.Rows.Count).End(xlUp).Row + 1
        For j = 2 To UBound(a)
            If Not dd.exists(a(j, 2)) Then
                s2.Cells(nx, 1).Value = s2.Cells(nx - 1, 1).Value + 1
                s2.Cells(nx, 2).Value = a(j, 2)
                s2.Cells(nx, 3).Value = a(j, 3)
                s2.Cells(nx, 4).Value = Chr(251)
                s2.Cells(nx, 5).Value = -a(j, 3)
                s2.Range("A" & nx & ":E" & nx).Interior.ColorIndex = 3 ' red 10 ' green
                s2.Range("A" & nx & ":E" & nx).Borders.Weight = xlThin
                s2.Range("A" & nx & ":D" & nx).HorizontalAlignment = xlCenter
            nx = nx + 1
            End If
        Next j
    End Sub

  5. #5
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    Thanks !
    may you check file ,please?
    after I run the macro there is problem in column D for row14 doesn't show wrong mark and values in column E and highlight the row .
    and row 16,17 there is no wrong mark . as to first sheet should also highlight new items as I did it in OP .
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: vba compare between two sheets and highlight new items

    Try this code.

    Sub UpdateSheet2w()
    Dim LR As Long, c As Range, k&, i&, j&, nx&, x&, r&
    Dim s1 As Worksheet, s2 As Worksheet
    Dim d As Object, dd As Object, a
    
    Set s1 = Sheets("sheet1")
    Set s2 = Sheets("sheet2")
    Application.ScreenUpdating = False
    a = s1.[A1].Offset(1).CurrentRegion
    
    Set d = CreateObject("Scripting.Dictionary")
    Set dd = CreateObject("Scripting.Dictionary")
    d.CompareMode = vbTextCompare
        For i = LBound(a) To UBound(a)
            If Not d.exists(a(i, 2)) Then
                d.Add a(i, 2), k
                k = k + 1
            End If
        Next i
    For i = LBound(a) To UBound(a)
        For x = 2 To s2.Range("B" & s2.Rows.Count).End(xlUp).Row
            If Not dd.exists(s2.Cells(x, 2).Value) Then
                dd.Add s2.Cells(x, 2).Value, r
                r = r + 1
            End If
            If s2.Cells(x, 2).Value = a(i, 2) Then
                s2.Cells(x, 5).Value = s2.Cells(x, 3).Value - a(i, 3)
                If s2.Cells(x, 5).Value = 0 Then
                    s2.Cells(x, 4).Value = Chr(252)
                Else
                    s2.Cells(x, 4).Value = Chr(251)
                End If
            x = x + 1
            End If
        Next
    Next
        nx = s2.Range("B" & s2.Rows.Count).End(xlUp).Row + 1
        For j = 2 To UBound(a)
            If Not dd.exists(a(j, 2)) Then
                s2.Cells(nx, 1).Value = s2.Cells(nx - 1, 1).Value + 1
                s2.Cells(nx, 2).Value = a(j, 2)
                s2.Cells(nx, 3).Value = a(j, 3)
                s2.Cells(nx, 4).Value = Chr(251)
                s2.Cells(nx, 4).Font.Name = "Wingdings"
                s2.Cells(nx, 5).Value = -a(j, 3)
                s2.Range("A" & nx & ":E" & nx).Interior.ColorIndex = 3 ' red 10 ' green
                s2.Range("A" & nx & ":E" & nx).Borders.Weight = xlThin
                s2.Range("A" & nx & ":D" & nx).HorizontalAlignment = xlCenter
            nx = nx + 1
            End If
        Next j
    For x = 2 To s2.Range("B" & s2.Rows.Count).End(xlUp).Row
        If IsEmpty(s2.Cells(x, 5)) Then
            s2.Cells(x, 4).Value = Chr(251)
            s2.Cells(x, 4).Font.Name = "Wingdings"
            s2.Cells(x, 5).Value = s2.Cells(x, 3).Value
            s2.Range("A" & x & ":E" & x).Interior.ColorIndex = 43 ' lt green
        End If
    Next
    
    Application.ScreenUpdating = True
    End Sub
    Last edited by maniacb; 03-10-2023 at 05:42 PM.

  7. #7
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    thanks again !
    in column D for red ranges should show minus values when subtract column C for sheet2 from column C for sheet1 , then will show minus values , not zero as the code does it and should show wrong mark in column D , not right mark as the code does it , you can compare with expected result for columns I:M
    I attached file after testing to see the errors.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: vba compare between two sheets and highlight new items

    I see the errors in the file you provide but can't recreate those results when I run the code. Please let me know if the issue persists.

  9. #9
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    I see the errors in the file you provide but can't recreate those results when I run the code. Please let me know if the issue persists.
    do you mean the errors doesn't show for you?!
    actually the errors are still showing .

  10. #10
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    ok this is because of running the macro repeatedly . data could change or update in two sheets , so how fix it ,please?

  11. #11
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: vba compare between two sheets and highlight new items

    Try this update.

    Sub UpdateSheet2x()
    Dim LR As Long, c As Range, k&, i&, j&, nx&, x&, r&, n&, xx&
    Dim s1 As Worksheet, s2 As Worksheet
    Dim d As Object, dd As Object, a
    
    Set s1 = Sheets("sheet1")
    Set s2 = Sheets("sheet2")
    Application.ScreenUpdating = False
    a = s1.[A1].Offset(1).CurrentRegion
    n = s2.Range("B" & s2.Rows.Count).End(xlUp).Row
    For i = n To 1 Step -1
        If s2.Cells(i, 1).Interior.ColorIndex = 3 Then '3 = Red
            s2.Rows(i).Delete
        End If
    Next
    xx = s2.Range("B" & s2.Rows.Count).End(xlUp).Row
    Set d = CreateObject("Scripting.Dictionary")
    Set dd = CreateObject("Scripting.Dictionary")
    d.CompareMode = vbTextCompare
        For i = LBound(a) To UBound(a)
            If Not d.exists(a(i, 2)) Then
                d.Add a(i, 2), k
                k = k + 1
            End If
        Next i
    For i = LBound(a) To UBound(a)
        For x = 2 To xx
            If Not dd.exists(s2.Cells(x, 2).Value) Then
                dd.Add s2.Cells(x, 2).Value, r
                r = r + 1
            End If
            If s2.Cells(x, 2).Value = a(i, 2) Then
                s2.Cells(x, 5).Value = s2.Cells(x, 3).Value - a(i, 3)
                If s2.Cells(x, 5).Value = 0 Then
                    s2.Cells(x, 4).Value = Chr(252)
                Else
                    s2.Cells(x, 4).Value = Chr(251)
                End If
                s2.Cells(x, 4).Font.Name = "Wingdings"
            x = x + 1
            End If
        Next
    Next
        nx = xx + 1
        For j = 2 To UBound(a)
            If Not dd.exists(a(j, 2)) Then 'And s2.Cells(nx, 1).Interior.ColorIndex <> 3 Then
                s2.Cells(nx, 1).Value = s2.Cells(nx - 1, 1).Value + 1
                s2.Cells(nx, 2).Value = a(j, 2)
                s2.Cells(nx, 3).Value = a(j, 3)
                s2.Cells(nx, 4).Value = Chr(251)
                s2.Cells(nx, 4).Font.Name = "Wingdings"
                s2.Cells(nx, 5).Value = -a(j, 3)
                s2.Range("A" & nx & ":E" & nx).Interior.ColorIndex = 3 ' red 10 ' green
                s2.Range("A" & nx & ":E" & nx).Borders.Weight = xlThin
                s2.Range("A" & nx & ":D" & nx).HorizontalAlignment = xlCenter
            nx = nx + 1
            End If
        Next j
    For x = 2 To nx - 1
        If IsEmpty(s2.Cells(x, 5)) Or s2.Range("A" & x & ":E" & x).Interior.ColorIndex = 43 Then
            s2.Cells(x, 4).Value = Chr(251)
            s2.Cells(x, 4).Font.Name = "Wingdings"
            s2.Cells(x, 5).Value = s2.Cells(x, 3).Value
            s2.Range("A" & x & ":E" & x).Interior.ColorIndex = 43 ' lt green
        End If
    Next
    Application.ScreenUpdating = True
    End Sub
    Last edited by maniacb; 03-12-2023 at 02:54 PM.

  12. #12
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    thanks
    now everything is ok , just I have question when update the data in sheet and become the changed data are matched with data have already highlighted before , then should delete the color to become xl None or delete the whole red highlighted range when become matched after update or change between two sheets , is there way to do this case?
    Last edited by leap out; 03-13-2023 at 05:46 AM.

  13. #13
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: vba compare between two sheets and highlight new items

    The updated code actually deletes the rows in red on sheet2 and recreates those lines just in case those have changed. You do not have to manually delete those lines. The green lines are also updated if column c is changed. Try both those cases and let me know if there are any discrepancies.

  14. #14
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    what happened for me something is strange !( it will not highlight new items by green despite of there are existed in sheet2 but it's not existed in sheet1 for items 13,16) .
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: vba compare between two sheets and highlight new items

    Line items on sheet2 will only change green if columns D and E are empty in a row when the code runs. The code is looking for any bypassed items(no match with sheet1) and then highlights them green. If there is data in the last two columns of that row the code assumes it was populated because that item existed on sheet1. Does that better explain the process for you?

  16. #16
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: vba compare between two sheets and highlight new items

    Line items on sheet2 will only change green if columns D and E are empty in a row when the code runs. The code is looking for any bypassed items(no match with sheet1) and then highlights them green. If there is data in the last two columns of that row the code assumes it was populated because that item existed on sheet1. Does that better explain the process for you?
    I'm really sorry !
    I lost my concentration because of posting many answering .
    I appreciate for your effort & time.
    many thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to compare sheets to find missing items
    By nates808 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 12-17-2022, 05:45 PM
  2. [SOLVED] Highlight & copy new items across sheets
    By KalilMe in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 07-18-2022, 10:39 AM
  3. Compare Two Sheets and Highlight the Difference
    By amrans20 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2022, 05:14 AM
  4. Compare Two Sheets and Highlight Common Items
    By farhadj in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-06-2019, 09:06 AM
  5. [SOLVED] Compare Two Sheets and Highlight Differences
    By Blake 7 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-12-2015, 11:44 AM
  6. [SOLVED] Compare name list (First name, last name) on two different sheets and highlight
    By badboynads in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-25-2013, 07:37 AM
  7. Formula to Compare and Replace Numbers Between two Sheets
    By leo2308 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 09-18-2009, 06:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1