+ Reply to Thread
Results 1 to 8 of 8

Macro to Compare Data on two sheets

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Macro to Compare Data on two sheets

    I have code below that I tried to adapt per items 1 & 2 below but cannot get the code to give me the correct result


    1) Where the Reference in Col A and the value in Col D on Sheet ?Statement Current Month? does not match the Reference in Col F and value in Col J on sheet ?Purchase Ledger?, then the unmatched items to be extracted to sheet ?Statement Recon Items?
    2) Where the Reference in Col F and the value in Col J on Sheet ?Purchase Ledger? does not match the Reference in Col A and value in Col D on sheet ?Statement Current Month? , then the unmatched items to be extracted to sheet ?PL Recon Items?


    It would be apprciated if someone could either amend my code or provide new code to accomodate my request

    Please Login or Register  to view this content.
    Last edited by Howardc1001; 02-08-2023 at 10:21 PM.

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Macro to Compare Data on two sheets

    One way:
    PHP Code: 
    Option Explicit
    Sub ReconItemsList
    ()
    Dim lr&, i&, j&, rngarST(), arPL()

    ' delete old sheet
    If Evaluate("=ISREF('
    Statement Recon Items'!A1)") Then Sheets("Statement Recon Items").Delete
    If Evaluate("=ISREF('
    PL Recon Items'!A1)") Then Sheets("PL Recon Items").Delete

    '
    dupplicate sheet
    Sheets
    ("Statement Current Month").Copy after:=Sheets(Sheets.Count)
    ActiveSheet.Name "Statement Recon Items"
    Sheets("Purchase Ledger").Copy after:=Sheets(Sheets.Count)
    ActiveSheet.Name "PL Recon Items"

    'copy data into array
    With Sheets("Statement Recon Items")
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        arST = .Range("A2:G" & lr).Value
    End With
    With Sheets("PL Recon Items")
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        arPL = .Range("A2:L" & lr).Value
    End With

    '
    check each row in both array, if both match then mark last column (helper columnof both arrays with "error sign"
    For 1 To UBound(arST)
        For 
    1 To UBound(arPL)
            If 
    arST(i1) = arPL(j6) And arST(i4) = arPL(j10Then
                arST
    (i7) = Evaluate("=1/0"): arPL(j12) = Evaluate("=1/0")
            
    End If
        
    Next
    Next

    ' paste array back to sheet, then delete rows those with "error sign"
    With Sheets("Statement Recon Items")
        .Range("A2:G10000").ClearContents
        .Range("A2").Resize(UBound(arST), 7).Value = arST
        .Range("G2:G10000").SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
    End With
    With Sheets("PL Recon Items")
        .Range("A2:L10000").ClearContents
        .Range("A2").Resize(UBound(arPL), 12).Value = arPL
        .Range("L2:L10000").SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
    End With
    End Sub 
    Quang PT

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Compare Data on two sheets

    Many Thanks for the help. Your code works 100%

    I have tried to understand the logic behind yoir code and I understand everything except the code below

    Please explain how this works

    Please Login or Register  to view this content.

  4. #4
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Macro to Compare Data on two sheets

    Quote Originally Posted by Howardc1001 View Post
    Many Thanks for the help. Your code works 100%
    I have tried to understand the logic behind yoir code and I understand everything except the code below
    Please explain how this works
    Nice to know that you are willing to learn!
    The best way is, remove the 2 lines those delete helper column
    PHP Code: 
    ...
    .
    Range("G2:G10000").SpecialCells(xlCellTypeConstantsxlErrors).EntireRow.Delete
    ...
     .
    Range("L2:L10000").SpecialCells(xlCellTypeConstantsxlErrors).EntireRow.Delete 
    Then easy to see what is written in those columns!

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Macro to Compare Data on two sheets

    Advanced filter
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Compare Data on two sheets

    Thanks bebo021999

  7. #7
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Compare Data on two sheets

    Many Thanks Jindon. Your code works perfectly

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Macro to Compare Data on two sheets

    You are welcome and thanks for the rep.

+ 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. Replies: 1
    Last Post: 07-23-2015, 10:15 PM
  2. Replies: 0
    Last Post: 05-06-2015, 01:10 PM
  3. need a macro to compare two ranges in two different sheets
    By shahidjafri2004 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2014, 07:35 AM
  4. Macro for compare two sheets based on sheet3 reference data of sheet1
    By aniletc37 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-08-2014, 04:09 AM
  5. [SOLVED] Macro to compare data between sheets and copy/paste
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-24-2012, 05:46 PM
  6. macro to compare 2 sheets row wise and copy unmatched data to 3rd sheet
    By prachi b in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-07-2012, 01:46 AM
  7. Help With Macro To Compare Sheets
    By gaspower in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-14-2011, 02:51 PM

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