+ Reply to Thread
Results 1 to 9 of 9

Comparing two excel files and copy Unmatched

Hybrid View

  1. #1
    Registered User
    Join Date
    01-08-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2010
    Posts
    11

    Comparing two excel files and copy Unmatched

    Hello!

    I am a newbie here and in VBA.

    I have two excel files a "weekly" WB and a report with "main data" WB.

    I need to compare "weekly" WB "Raw data" sheet Column A with "main data" WB "report" sheet Column E (starting from E6). If the data matching then I need to copy from "weekly" report "Raw data" sheet D column data (where the compared cells are matching) to "main data" WB "report" sheet column D.

    Also if there are unmatched data then unmatched data needs to copy to the end of "main data" WB "report" sheet.

    Any help would be appreciated!

    Thanks!

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Comparing two excel files and copy Unmatched

    Welcome to the Forum Peter. Can you attach a sample file with some dummie data to test on? To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

  3. #3
    Registered User
    Join Date
    01-08-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2010
    Posts
    11

    Thumbs up Re: Comparing two excel files and copy Unmatched

    Hi John!

    Thank you for the greetings! http://www.excelforum.com/images/icons/icon14.png

    I have figured out the main part of the code. I have looked after on many forums and with a "little" assistance I have created the following code:

    Sub matchReport()
        Dim id As String
        Dim cmo_fmo As String
        Dim last_row, j As Integer
                
        ' get last row of this workbook
        last_row = ActiveSheet.UsedRange.Rows.Count + 1
                    
        ' MODIFY THIS ROW
        Set Source = Workbooks.Open("c:\3_-_Munka\Network\LAN\EXCEL FILE NAME")
        
        
        j = 2
        Do While (Source.Sheets("Raw data").Cells(j, 1).Value <> "")
        
            ' get values from source
            id = Source.Sheets("Raw data").Cells(j, 1).Value
            cmo_fmo = Source.Sheets("Raw data").Cells(j, 4).Value
                
            'find id
            ThisWorkbook.Activate
            Columns("E:E").Select
            Set cell = Selection.Find(What:=id, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
        
            If cell Is Nothing Then
                'if id not found -> insert row from source
                ActiveSheet.Cells(last_row, 1).Value = Source.Sheets("Raw data").Cells(j, 5).Value
                ActiveSheet.Cells(last_row, 2).Value = Source.Sheets("Raw data").Cells(j, 2).Value
                ActiveSheet.Cells(last_row, 3).Value = Source.Sheets("Raw data").Cells(j, 3).Value
                ActiveSheet.Cells(last_row, 4).Value = Source.Sheets("Raw data").Cells(j, 4).Value
                ActiveSheet.Cells(last_row, 5).Value = Source.Sheets("Raw data").Cells(j, 1).Value
                ActiveSheet.Cells(last_row, 6).Value = Source.Sheets("Raw data").Cells(j, 10).Value
                ActiveSheet.Cells(last_row, 5).Value = Source.Sheets("Raw data").Cells(j, 1).Value
                
                'inc last row
                last_row = last_row + 1
            Else
                'match found -> paste CMO / FMO value
                ActiveSheet.Cells(cell.Row, 4).Value = cmo_fmo
            End If
            
            j = j + 1
        Loop
    
        Source.Close
        
    End Sub
    Only one task remained but first I try to figure out then ask questions
    Last edited by peter.hernadi; 08-27-2013 at 02:53 AM.

  4. #4
    Registered User
    Join Date
    01-08-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Comparing two excel files and copy Unmatched

    While I am searching the next task would be -

    If Column 'A' in "workbook name" "report" sheet contains :
    • eoi then the data in column D (in the same row) will be 'FMO'
    • SW then the data in column D (in the same row) will be 'CMO'

    and this action is in loop until it reaches the last row. Maybe I can build it into the original code but maybe it would be better put in a separate sub.

    sample.jpg

    thanks in advance!
    Last edited by peter.hernadi; 08-27-2013 at 02:53 AM.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Comparing two excel files and copy Unmatched

    Peter, though I think it would be best to attach an actual workbook with sample data. Maybe this?

    Sub peterhernadi()
    Dim i As Long
    Workbooks("report.xls").Activate
    Sheets("Sheet1").Select
    For i = ActiveSheet.UsedRange.Rows.count To 2 Step -1
        Select Case Range("A" & i).Value
            Case Is = "eoi"
                Range("D" & i).Value = "FMO"
            Case Is = "SW"
                Range("D" & i).Value = "CMO"
        End Select
    Next i
    End Sub

  6. #6
    Registered User
    Join Date
    01-08-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Comparing two excel files and copy Unmatched

    Yes, it would be easier, but I needed to create a spreadsheet with sample data.

    It is attached now and thank you!

    Sample_CMO-FMO.xlsx

+ 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] Comparing two worksheets and highlight unmatched Data
    By vignesh rocks in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-27-2012, 03:45 PM
  2. Comparing two sheets and highlight unmatched data
    By vignesh rocks in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2012, 02:05 PM
  3. Compare sheets and copy unmatched cells
    By praetorianprefect in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-29-2011, 01:03 AM
  4. Compare 2 sheets & copy unmatched data below
    By tek9step in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-27-2010, 10:57 AM
  5. VBA - Compare one column in two sheets & copy unmatched to one sheet
    By TreasureCat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-19-2007, 11:18 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