+ Reply to Thread
Results 1 to 1 of 1

insert data from child workbook to master workbook by search matching records

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2011
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    2

    Smile insert data from child workbook to master workbook by search matching records

    Hi,

    I'm a newbie in VBA, search alot of post about how to copy data from child workbook to master workbook by searching matching records without overwritng the existing data.

    What i was trying to do is copy all the records from child workbook by searching matching records(company code) from the master workbook. After found the matching records then it will insert the data from child workbook to master workbook.

    I was only able to insert the data at the end of the rows. Can anyone help me write marco on how to search matching records then insert the whole row into the matching records.


    Sub Merge()
        Dim i As Integer
        Dim Source As Workbook
        Dim Destination As Workbook
        Dim myFileName As String
         
         
         
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Application.EnableEvents = False
         
        On Error Resume Next
         
        Set Destination = ThisWorkbook
         
         
        With Application.FileSearch
            .NewSearch
             'Change path to suit
            .LookIn = "C:\Documents and Settings\Administrator\Desktop\test\slave"
            .FileType = msoFileTypeExcelWorkbooks
             
             
            If .Execute > 0 Then 'Workbooks in folder
                For i = 1 To 3 '  Loop through all
                     'Set Paste target first
                    myFileName = Dir(.FoundFiles(i))
                    
                    'Open Souce file
                    Set Source = Workbooks.Open(.FoundFiles(i))
                    With Destination.Sheets(1)
                         '''''' Here I would then have to make an if to do a different selection based on what input file I have
                        Select Case myFileName
                        Case "Slave1.xls"
                            'Here selection for first file
                        Case "Slave2.xls"
                            'Here selection for second file
                        End Select
                        Set Tgt = .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
                    End With
                     'Copy and paste to destination
                    Source.Sheets(1).Range("A4:D20").Copy Tgt
                     'Close source file without  saving
                    Source.Close False
                Next i
                 
            End If
             
        End With
         
        On Error GoTo 0
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.CutCopyMode = False
    End Sub
    Please help!!!
    Attached Files Attached Files
    Last edited by jjgal767; 04-25-2011 at 03:37 AM. Reason: add code tags for newbie PM rules

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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