+ Reply to Thread
Results 1 to 14 of 14

Compare Two workbooks

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-21-2012
    Location
    Ind
    MS-Off Ver
    Excel 2010
    Posts
    110

    Compare Two workbooks

    Hey guys!

    I have this task which is driving me crazy!

    Now my task looks like this: I have to compare two excel workbooks. One contains old data, and the other new data. Now I need to compare every worksheet from the file with new data. Sometimes there are lot of sheets & sometimes few sheets. If there is mismatch in data then code should bring that data into new workbook.


    So far I have found below code. But it’s really not working Please advise.

    Sub Compare()
         
        Dim wbkComp As Workbook
        Dim wbkWith As Workbook
        Dim wbkDiff As Workbook
        Dim shtComp As Worksheet
        Dim shtWith As Worksheet
        Dim shtDiff As Worksheet
        Dim lngCompRow As Long
        Dim lngDiffRow As Long
        Dim blnSame As Boolean
        Dim intCol As Integer
        MyFileName = Application.GetOpenFilename
        MyFilework = Application.GetOpenFilename
         
        Set wbkComp = Workbooks.Open(Filename:=MyFileName)
        Set wbkWith = Workbooks.Open(Filename:=MyFilework)
        Set wbkDiff = Workbooks.Add
         
        For Each shtComp In wbkComp.Worksheets
            Application.StatusBar = "Checking " & shtComp.Name
            Set shtWith = wbkWith.Worksheets(shtComp.Name)
            Set shtDiff = wbkDiff.Worksheets.Add
            shtDiff.Name = "Diff " & shtComp.Name
            lngCompRow = 1
            lngDiffRow = 1
            Do While shtComp.Cells(lngCompRow, 1) <> ""
                blnSame = True
                For intCol = 1 To 3
                    If shtComp.Cells(lngCompRow, intCol) <> shtWith.Cells(lngCompRow, intCol) Then
                        blnSame = False
                        Exit For
                    End If
                Next
                If Not blnSame Then
                    shtComp.Rows(lngCompRow).Copy shtDiff.Cells(lngDiffRow, 1)
                    lngDiffRow = lngDiffRow + 1
                End If
                lngCompRow = lngCompRow + 1
            Loop
        Next
        Application.StatusBar = False
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    Hi Akulka,

    What does your data look like and is it the same field by field and are there key fields? and,and ....
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    10-21-2012
    Location
    Ind
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Compare Two workbooks

    Data differ each times. Sometimes I got A:F column data. Sometimes its hugh. Another thing I would like to tell you that both file have same filed & column. Volumes differs.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    Can you post a couple of sample spreadsheets?

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are demonstrated, mock them up manually if needed. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  5. #5
    Forum Contributor
    Join Date
    10-21-2012
    Location
    Ind
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Compare Two workbooks

    I have attached 3 workbooks where I explained my issue. The OUTPUT file gives you idea what I am trying to do. It is not mandatory that I need output what I mentioned over there. You can suggest me more good idea. At the end of the day I need to know where is data getting changed in both file so that I can pass in data quality.
    Attached Files Attached Files

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    Hi Akulka,

    Must the routine search for these tables or are they always in the same place?

    This works with your sample data:

    Sub Akulka(): Dim wt As Workbook, wy As Workbook, WO As Workbook
    Dim wtd As Worksheet, wyd As Worksheet, wod As Worksheet
    Dim r As Long, c As Long, lr As Long, lc As Long, i As Long, j As Long
    Dim T As String, Y As String, n As Integer, k As Long, WI, WF As Object
    WI = Array(" ", "DATA1", "DATA2", "DATA3"): lr = 1: lc = 1: r = 1: c = 1
            'Did you want to skip weekends and holidays?
    T = Replace(CStr(Date), "/", ""): Y = Replace(CStr(Date - 1), "/", "")
    Set wt = Workbooks(T & ".xlsx"): Set wy = Workbooks(Y & ".xlsx")
        Set WO = Workbooks("Output.xlsx"): Set WF = WorksheetFunction
                For n = 1 To wt.Worksheets.count: WI = "DATA" & n
            Set wtd = wt.Worksheets(WI): Set wyd = wy.Worksheets(WI)
                    Set wod = WO.Worksheets("diff" & WI)
    GetTableLimits: Do Until WF.CountA(wtd.Columns(c)) <> 0: c = c + 1
                        If c >= Columns.count Then
                        GoTo GetAnother: End If
                    Loop
                    Do Until WF.CountA(wtd.Rows(r)) <> 0: r = r + 1: Loop
                    lr = r: Do Until WF.CountA(wtd.Rows(lr + 1)) = 0: lr = lr + 1: Loop
                    lc = c: Do Until WF.CountA(wtd.Columns(lc + 1)) = 0: lc = lc + 1: Loop
                    For j = r + 1 To lr: For i = c + 1 To lc
                    If wtd.Cells(j, i) <> wyd.Cells(j, i) Then
                k = k + 1: wtd.Range(wtd.Cells(j, c), wtd.Cells(j, lc)).Copy wod.Cells(k, 1)
            wod.Cells(k, lc - c + 2) = wt.Name: wod.Cells(k, lc - c + 3) = "Row" & j
                    k = k + 1: wyd.Range(wyd.Cells(j, c), wyd.Cells(j, lc)).Copy wod.Cells(k, 1)
            wod.Cells(k, lc - c + 2) = wy.Name: wod.Cells(k, lc - c + 3) = "Row" & j
                    k = k + 2: GoTo GetNext: End If: Next i
    GetNext:                 Next j
                    c = lc + 3: GoTo GetTableLimits
    GetAnother: c = 1: lc = 1: r = 1: lr = 1: k = 0: Next n
    End Sub
    Last edited by xladept; 02-15-2014 at 05:51 PM.

  7. #7
    Forum Contributor
    Join Date
    10-21-2012
    Location
    Ind
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Compare Two workbooks

    This the main challenge because for each report table places are changed.

    I am getting error on Set wt = Workbooks(T & ".xlsx")
    Last edited by akulka58; 02-15-2014 at 06:51 PM.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    Hi Akulka,

    You need the set up that you sent me i.e.
    Today's Book, Yesterday's Book and a Book named Output


    Copy the code to the clipboard
    In the Book named Output
    Press ALT + F11 to open the Visual Basic Editor.
    Select Module from the Insert menu
    Type "Option Explicit" then paste the code into the white space on the right.

    With the cursor between Sub and End Sub press F5 OR

    Press ALT + Q to close the code window.
    Press ALT + F8 then double click on the macroname (Akulka)

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    Hi Akulka,

    Do you need instructions on how to run it?

  10. #10
    Forum Contributor
    Join Date
    10-21-2012
    Location
    Ind
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Compare Two workbooks

    Plz help me.

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    Oh - I forgot to tell you to save the Workbook as Macro-Enabled - sorry

  12. #12
    Forum Contributor
    Join Date
    10-21-2012
    Location
    Ind
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Compare Two workbooks

    Th code is throwing same error. Another thing is can we use Application.GetOpenFilename command because I do have 60 different reports to compare & their worksheet names not always DATA1, DATA2, DATA3.

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    I am getting error on Set wt = Workbooks(T & ".xlsx")
    If it's a subscript error then you don't have a book named 2152014.xlsx??

    I'll look into the GetOpenFilename option - but, it won't be today.

    Maybe another forum contributor can help you?

    And, thanks for the rep!
    Last edited by xladept; 02-16-2014 at 01:39 PM.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare Two workbooks

    Hi Akulka,

    My plans fell through - try this and let me know of the next issue

    Sub Akulka2(): Dim wt As Workbook, wy As Workbook, wo As Workbook
    Dim wtd As Worksheet, wyd As Worksheet, wod As Worksheet, FNT As String, FNY As String
    Dim r As Long, c As Long, lr As Long, lc As Long, i As Long, j As Long, WI As String
    Dim T As String, Y As String, n As Integer, k As Long, WF As Object
    lr = 1: lc = 1: r = 1: c = 1   'Did you want to skip weekends and holidays?
    n = Val(InputBox("How many days ago is the latest comparison file - 0,1,2,3 etc?"))
    T = Replace(CStr(Date - n), "/", ""): Y = Replace(CStr(Date - n - 1), "/", "")
    GetFiles: If n > 10 Then Exit Sub
    FNT = Application.GetOpenFilename: FNY = Application.GetOpenFilename
    VerifyFiles:
            If InStr(1, FNT, T) And InStr(1, FNY, Y) = 0 Then
            n = n + 1: GoTo GetFiles: End If
    Workbooks.Open FNT: Set wt = ActiveWorkbook: Workbooks.Open FNY: Set wy = ActiveWorkbook
         Set wo = Workbooks("Output.xlsx"): wo.Activate: Set WF = WorksheetFunction
                For n = 1 To wt.Worksheets.Count: WI = wt.Sheets(n).Name
            Set wtd = wt.Worksheets(WI): Set wyd = wy.Worksheets(WI)
                        For Each wod In wo.Worksheets
            If wod.Name = ("diff" & WI) Then GoTo Setwod
                        Next
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "diff" & WI
    Setwod:                Set wod = wo.Worksheets("diff" & WI)
    GetTableLimits: Do Until WF.CountA(wtd.Columns(c)) <> 0: c = c + 1
                        If c >= Columns.Count Then
                        GoTo GetAnother: End If
                    Loop
                    Do Until WF.CountA(wtd.Rows(r)) <> 0: r = r + 1: Loop
                    lr = r: Do Until WF.CountA(wtd.Rows(lr + 1)) = 0: lr = lr + 1: Loop
                    lc = c: Do Until WF.CountA(wtd.Columns(lc + 1)) = 0: lc = lc + 1: Loop
                    For j = r + 1 To lr: For i = c + 1 To lc
                    If wtd.Cells(j, i) <> wyd.Cells(j, i) Then
                k = k + 1: wtd.Range(wtd.Cells(j, c), wtd.Cells(j, lc)).Copy
                wod.Cells(k, 1).PasteSpecial xlPasteValues
            wod.Cells(k, lc - c + 2) = wt.Name: wod.Cells(k, lc - c + 3) = "Row" & j
                k = k + 1: wyd.Range(wyd.Cells(j, c), wyd.Cells(j, lc)).Copy
                wod.Cells(k, 1).PasteSpecial xlPasteValues
            wod.Cells(k, lc - c + 2) = wy.Name: wod.Cells(k, lc - c + 3) = "Row" & j
                    k = k + 2: GoTo GetNext: End If: Next i
    GetNext:                 Next j
                    c = lc + 3: GoTo GetTableLimits
    GetAnother: c = 1: lc = 1: r = 1: lr = 1: k = 0: Next n
    End Sub

+ 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. Compare two workbooks
    By mattwellings in forum Excel General
    Replies: 0
    Last Post: 07-14-2011, 10:09 AM
  2. Compare workbooks
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-31-2011, 02:35 PM
  3. Compare two Workbooks
    By MBCMDR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-26-2009, 09:11 AM
  4. Compare Workbooks VBA - Please Help
    By bkeller83 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-28-2009, 01:19 AM
  5. [SOLVED] COMPARE 2 WORKBOOKS
    By dgr in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2005, 06:10 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