+ Reply to Thread
Results 1 to 10 of 10

File Compare Macro

Hybrid View

JoanneA File Compare Macro 05-23-2013, 02:26 PM
p24leclerc Re: File Compare Macro 05-26-2013, 03:44 PM
JoanneA Re: File Compare Macro 05-27-2013, 02:14 PM
p24leclerc Re: File Compare Macro 05-27-2013, 10:02 PM
JoanneA Re: File Compare Macro 05-28-2013, 11:47 AM
p24leclerc Re: File Compare Macro 05-28-2013, 04:50 PM
JoanneA Re: File Compare Macro 05-28-2013, 05:05 PM
JoanneA Re: File Compare Macro 05-28-2013, 06:12 PM
p24leclerc Re: File Compare Macro 05-28-2013, 08:47 PM
JoanneA Re: File Compare Macro 05-29-2013, 12:55 PM
  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    Campobello, SC
    MS-Off Ver
    Excel 2007
    Posts
    8

    File Compare Macro

    I have seen posts referring to a file compare macro. I need to compare two files, one new and one old, and add 2 tabs to the new one that shows the items added on one tab, and the items deleted on the other, with respect to the old file. Can someone help me, please?

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: File Compare Macro

    Certainly. Can you attached sample files to you thread so we can work this out? Please give some instructions on how to compare both files.
    Regards
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Registered User
    Join Date
    05-23-2013
    Location
    Campobello, SC
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: File Compare Macro

    I need to compare two sepapate wooksheets, one old and one new. (I can't get the format to show up properly, so I placed '|' between the fields.) These are a sample of the records; the files are much larger.

    This is the old file to be compared:

    Data Set Key Name| File Type| last ref| last job
    XP2.SMS.A0HR0.P20.MNTRUN| GDG BASE| 22APR13:13:34:41| XP2AHRBP
    XP2.SMS.A0HR0.V52.JHRB6.FILE01.DY| GDG BASE| 01DEC09:11:50:43| XP2AHR18
    XP2.SMS.A0HR0.V52.JHRB6.FILE02.DY| GDG BASE| 01DEC09:11:51:02| XP2AHR18
    XP2.SMS.A0HR0.V52.JHRB7.FILE01.DY| GDG BASE| 01DEC09:11:31:58| XP2API2P
    XP2.SMS.A0HR0.V52.JHRB7.FILE02.DY| GDG BASE| 01DEC09:11:32:07| XP2API2P
    XP2.SMS.A0HR0.V52.JHRQI.FILE01.QT| GDG BASE| 09OCT09:16:37:27| XP2AHRQI
    XP2.SMS.A0HR0.V52.JHRYB.FILE01.YE08| GDG BASE| 05JAN09:13:44:18| XP2AHRYO
    XP2.SMS.A0HR0.V52.JHRYB.FILE02.YE08| GDG BASE| 05JAN09:13:45:57| XP2AHRYO
    XP2.SMS.A0HR0.V60.JHRBM.FILE01.BM| GDG BASE| 22OCT12:21:25:31| XP2AHRBM
    XP2.SMS.A0HR0.V60.JHRBM.FILE02.BM| GDG BASE| 22OCT12:21:25:31 | XP2AHRBM


    This is the new file:

    AppName| DSNAME| BACKUP Method| GDG| First Backup| Latest Backup| Jobname| disp| type
    NPPAYROL|XP2.SMS.A0HR0.P20.MNTRUN| TAPECPY| G| 06APR09:00:14:26| 22FEB11:17:41:50| XP2AHR06| OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.H20.MASTER02| TAPECPY| G| 15AUG07:00:38:48| 22FEB11:11:20:28| XP2AHR07| OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.V52.JHRQA.FILE01.QT| TAPECPY| G| | XPAHRQA OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.V52.JHRQA.FILE02.QT| TAPECPY| G| | XPAHRQA OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.V60.JHRQA.FILE01.QT| TAPECPY| G| | XP2AHRQA| OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.V60.JHRQA.FILE02.QT| TAPECPY| G| | XP2AHRQA| OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.V61.JHRQA.FILE01.QT| TAPECPY| G| | XP2AHRQA| OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.V61.JHRQA.FILE02.QT| TAPECPY| G| | XP2AHRQA| OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.JHRQA.P20.MNTRUN.QT| TAPECPY| G| | XP2AHRQA| OLD| INPUT
    NPPAYROL|XP2.SMS.A0HR0.JHRQB.P20.EXTRT.QTRLY| TAPECPY| G| 26OCT07:00:23:34| 15JAN11:16:03:37| XP2AHRQB| OLD| INPUT


    The lines that have NPPAYROL in the AppName (there are other AppNames) on the NEW file are the only ones we need to be concerned with, and will drive the compare, since there are no AppNames on the old file.

    When ccomparing the DSNAME on the two files, if one is in the OLD file and not in the new one, then it needs to be put on another sheet with the tab JOBS REMOVED.

    If one is in the NEW file and not in the old one, it needs to be put on another sheet with the tab JOBS ADDED.

    These new tabs need to be added to the NEW file workbook.

    Let me know if you need more information.

    Thank you so much!!

    JoanneA
    Last edited by JoanneA; 05-27-2013 at 02:33 PM.

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: File Compare Macro

    Here is a macro and 2 files that could help you.
    The macro is based on the file's names and sheets' names also. If you have to change this, do not forget to modify the code accordingly.
    The macro is in New-file.xlsm workbook. The Old-file.xlsm workbook has no macro in it and only one sheet of data. Both workbooks should be open before running the macro.
    You start the macro when you are in the New-file sheet.
    Not knowing waht to do with the ADDED or REMOVED jobs, I left them in place but they can also be deleted.
    Public Sub Check_Files()
    Dim Sh_New As Worksheet, Sh_Old As Worksheet, C_ell As Range, To_Copy As Range, F_ound As Range
    Dim Sh_added As Worksheet, Sh_Removed As Worksheet
    Set Sh_New = ActiveSheet
    Set Sh_Old = Workbooks("Old-File.xlsm").Sheets(1)
    '
    'Check if Jobs Added sheet exists
    On Error Resume Next
    Set Sh_added = Sheets("Jobs Added")
    On Error GoTo 0
    If Sh_added Is Nothing Then
      Sheets.Add After:=Sheets(Sheets.Count)
      Sheets(Sheets.Count).Select
      Sheets(Sheets.Count).Name = "Jobs Added"
    End If
    Set Sh_added = Sheets("Jobs Added")
    
    'Check if Jobs Removed sheet exists
    On Error Resume Next
    Set Sh_Removed = Sheets("Jobs Removed")
    On Error GoTo 0
    If Sh_added Is Nothing Then
      Sheets.Add After:=Sheets(Sheets.Count)
      Sheets(Sheets.Count).Select
        Sheets(Sheets.Count).Name = "Jobs Removed"
    End If
    Set Sh_Removed = Sheets("Jobs Removed")
    '
    'Look for JOBS ADDED
    Sh_New.Activate
    For Each C_ell In Range("B2", Cells(Rows.Count, 2).End(xlUp))
      Set F_ound = Sh_Old.Columns(1).Find(C_ell)
      If F_ound Is Nothing Then
        If To_Copy Is Nothing Then
          Set To_Copy = C_ell.EntireRow
        Else
          Set To_Copy = Union(To_Copy, C_ell.EntireRow)
        End If
      End If
    Next
    If Not To_Copy Is Nothing Then
      To_Copy.Copy
      Sh_added.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
    End If
    Set To_Copy = Nothing
    '
    'Look for JOBS REMOVED
    Sh_Old.Activate
    For Each C_ell In Range("A2", Cells(Rows.Count, 1).End(xlUp))
      Set F_ound = Sh_New.Columns(2).Find(C_ell)
      If F_ound Is Nothing Then
        If To_Copy Is Nothing Then
          Set To_Copy = C_ell.EntireRow
        Else
          Set To_Copy = Union(To_Copy, C_ell.EntireRow)
        End If
      End If
    Next
    If Not To_Copy Is Nothing Then
      To_Copy.Copy
      Sh_Removed.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
    End If
    
    End Sub
    Hope this helps
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    05-23-2013
    Location
    Campobello, SC
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: File Compare Macro

    This is great. Thank you. There is only one thing that I need to add. This has to be done only if Column A has NPPAYROL, otherwis it should be skipped. If you can tell me how it should be coded, I can add it.

    Again, thank you!

  6. #6
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: File Compare Macro

    Bonjour Joanne,
    Sorry, I knew I missed something but couldn't tell what exactly.
    Here is the modification to make. I thought it would be easier to change the whole JOBS ADDED section:
    'Look for JOBS ADDED
    Sh_New.Activate
    For Each C_ell In Range("B2", Cells(Rows.Count, 2).End(xlUp))
      Set F_ound = Sh_Old.Columns(1).Find(C_ell)
      If F_ound Is Nothing And C_ell.Offset(0, -1) = "NPPAYROL" Then 'HERE IS THE MODIFICATION
        If To_Copy Is Nothing Then
          Set To_Copy = C_ell.EntireRow
        Else
          Set To_Copy = Union(To_Copy, C_ell.EntireRow)
        End If
      End If
    Next
    If Not To_Copy Is Nothing Then
      To_Copy.Copy
      Sh_added.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
    End If
    Set To_Copy = Nothing
    '
    Regards

  7. #7
    Registered User
    Join Date
    05-23-2013
    Location
    Campobello, SC
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: File Compare Macro

    Thank you, Pierre. I will make that change.

  8. #8
    Registered User
    Join Date
    05-23-2013
    Location
    Campobello, SC
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: File Compare Macro

    I ran it and got good results, then realized I needed to add a field before rerunning it. That field is ID, where each NPPAYROL job is numbered HR001, HR002, etc.
    I reset all the files, re-added the NPPAYROL section to the macro, and now I can't get it to run again. These are the errors I get:

    Run-time error '9':
    Subscript out of range.


    And when I hit Debug, it highlights the line:

    Set Sh_Old = Workbooks("Old-File.xlsm").Sheets(1)

    I don't know what I did to it, but can you help, please?

  9. #9
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: File Compare Macro

    this means Excel cannot find the workbook "Old-file.xlsm". Are you sure this workbook was open when you ran the macro? If you still have the same error, can you attached the 2 files so I can take a look?

  10. #10
    Registered User
    Join Date
    05-23-2013
    Location
    Campobello, SC
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: File Compare Macro

    Thank you, Pierre. I can't believe it was as simple as that. I thought the macro had to open the Old-file.xlsm, so I didn't have it open. I tried it again with it open, and it runs perfectly. Sorry to be such a pest.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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