+ Reply to Thread
Results 1 to 3 of 3

Comparing Two Excel Workbooks & Output any differences

Hybrid View

  1. #1
    Registered User
    Join Date
    07-28-2013
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    3

    Comparing Two Excel Workbooks & Output any differences

    Hi experts,

    I am new to the forum, Nice to see a good community with so many experts of Excel programming & Macros,

    I want to compare two excel workbooks with multiple worksheets each having alphabets, numbers, texts in old file and new file.
    The names and order of sheets in old and new file is always the same just some data changes everyday and i want to check on daily basis which data has changed or there is no data changed at all.

    Below is the code that i am currently using and i have taken this code from external internet content,
    The problem is when i run this code it says unable to find file "olddata.xls" please check the path or correct the filename.

    Can you please help me to complete this operation?


    
    ' compare an old new to a new file using column B from each worksheet in the two workbooks
    ' if the old file contains a row that is not in the new file, then add the old data to the
    ' end of the new file.
    ' this assumes that the worksheets are all in the same order
    Sub CompareandMerge()
    
        Const oldFile As String = "olddata.xls"
        Const newFile As String = "newdata.xls"
    
        ' open the two workbooks
        Dim oldBook As Workbook
        Dim newbook As Workbook
        Set oldBook = Workbooks.Open(oldFile)
        Set newbook = Workbooks.Open(newFile)
    
        ' loop through all of the worksheets assuming both workbooks have the same number
        ' of workheets
        Dim i As Long
        For i = 1 To oldBook.Worksheets.Count
    
            Dim oldSheet As Worksheet
            Dim newSheet As Worksheet
            Set oldSheet = oldBook.Worksheets(i)
            Set newSheet = newbook.Worksheets(i)
    
            ' loop through all of the rows of the old sheet and insert missing rows at the end of the
            ' new sheet
            Dim oldRow As Range
            Dim newRow As Range
            Dim newRowCounter As Long
            newRowCounter = 1
            For Each oldRow In oldSheet.Rows
    
                ' stop when there is nothing in column B
                If (oldRow.Cells(1, "B").Value = "") Then
                    Exit For
                Else
    
                    ' get the new data at row newRowCounter
                    Set newRow = newSheet.Rows(newRowCounter)
    
                    ' compare the old data to the new data
                    ' if not equal, copy the current old row to the end of the new sheet
                    If (oldRow.Cells(1, "B").Value <> newRow.Cells(1, "B").Value) Then
    
                        Dim theNewRow As Long
                        theNewRow = newSheet.Range("B:B").Rows(newSheet.Range("B:B").Rows.Count).End(xlUp).Row + 1
                        oldRow.Copy newSheet.Rows(theNewRow)
    
                        ' hihglight the new row
                        With newSheet.Rows(theNewRow)
                            .Interior.ColorIndex = 6
                            .Interior.Pattern = xlSolid
                        End With
    
                    Else    ' increment to the next new row
                        newRowCounter = newRowCounter + 1
                    End If
                End If
            Next oldRow
        Next i
    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,866

    Re: [Help] Comparing Two Excel Workbooks & Output any differences

    Is the file with the macro in a different folder than the files you are comparing? If so you need to give the full path name for the files you want to open, similar to this:

    Const oldFile As String = "C:\My Documents\Data\olddata.xls"
    It is usually a good idea to use the full path names regardless, to avoid any ambiguity about what's going on.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    07-28-2013
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: [Help] Comparing Two Excel Workbooks & Output any differences

    Quote Originally Posted by 6StringJazzer View Post
    Is the file with the macro in a different folder than the files you are comparing? If so you need to give the full path name for the files you want to open, similar to this:

    Const oldFile As String = "C:\My Documents\Data\olddata.xls"
    It is usually a good idea to use the full path names regardless, to avoid any ambiguity about what's going on.
    Thanks for your kind support, The files were't located in Macro's folder,I would try this way.
    Cheers.

+ 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. Most Efficient Way in Comparing Two Workbooks and Consolidating Found Differences
    By quixter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2012, 12:46 PM
  2. Comparing two excel documents and coloring the differences part 2
    By jonathynblythe in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-04-2011, 03:01 PM
  3. Comparing two excel documents and coloring the differences
    By jonathynblythe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-07-2011, 11:16 AM
  4. Replies: 1
    Last Post: 11-15-2010, 12:24 PM
  5. [SOLVED] Comparing two workbooks and highlighting the differences
    By Charles C. in forum Excel General
    Replies: 2
    Last Post: 07-26-2006, 02:20 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