+ Reply to Thread
Results 1 to 3 of 3

Copy paste from one worbook to another workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2008
    Posts
    2

    Multiple workbooks

    This is my first post, and I'm in the early stages of learning Visual Basic, so go easy.

    I have several workbooks, all saved in the same folder, all in the same format but with different numbers of rows.

    I have the task of opening each workbook and copying the information into a 'master' workbook.

    I know that it's possible to write a program to do this, but don't have a clue where to start. Can anyone help? - I've bought a few books on Visual Basic, but they aren't very good.

  2. #2
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    Someone must have moved your post to a new thread.

    This simple example uses the FileSearch object to return a list of Excel files, then copies data from columns A & B in each one to the master sheet.

    I suggest you try this code out on a test workbook with a few test files all in their own folder. If you single step the code with the F8 key in the VB editor, you can see what's going on.
    Sub Read_Workbooks()
        Dim intFile As Integer
        Dim wsMaster As Worksheet
        Dim strThisWBName As String
        Dim strFileToOpen As String
        
        Application.ScreenUpdating = False
        
        strThisWBName = ThisWorkbook.Name
        Set wsMaster = ThisWorkbook.Worksheets(1)       'data will be copied to sheet1 of Master WB
        
        With Application.FileSearch
            .NewSearch                          'clear previous search
            .FileType = msoFileTypeExcelWorkbooks
            .LookIn = ThisWorkbook.Path        'look in same path as this workbook
            .SearchSubFolders = False
            If .Execute > 0 Then
                For intFile = 1 To .FoundFiles.Count
                    strFileToOpen = .FoundFiles(intFile)
                    If InStr(strFileToOpen, strThisWBName) = 0 Then 'check if trying to open this workbook
                        Transfer_Data strFileToOpen, wsMaster  'transfer from source worksheet to Master ws
                    End If
                Next
            Else
                MsgBox "No files found."
            End If
        End With
        
        Application.ScreenUpdating = True
        Set wsMaster = Nothing
    End Sub
    
    Sub Transfer_Data(sceFile As String, dstWS As Worksheet)
        'transfers first 2 columns of data from sceFile to the master sheet
        Dim intRow As Long
        Dim LastRowInDest As Long, LastRowInSce As Long
        Dim sceWB As Workbook
        Dim sceWS As Worksheet
        
        Application.EnableEvents = False            'suppress workbook_open event
        
        Set sceWB = Workbooks.Open(sceFile)
        Set sceWS = sceWB.Worksheets(1)
        
        LastRowInDest = dstWS.Cells(Rows.Count, "A").End(xlUp).Row 'get last used row in column A of master sheet
        LastRowInSce = sceWS.Cells(Rows.Count, "A").End(xlUp).Row   'get last used row in column A of source sheet
        
        'transfer data to summary sheet
        For intRow = 1 To LastRowInSce
            dstWS.Cells(LastRowInDest + intRow, 1).Value = sceWS.Cells(intRow, 1).Value   'transfer row intRow, column A
            dstWS.Cells(LastRowInDest + intRow, 2).Value = sceWS.Cells(intRow, 2).Value   'transfer row intRow, column B
        Next
        
        sceWB.Saved = True
        sceWB.Close
        
        Application.EnableEvents = True
        Set sceWS = Nothing
        Set sceWB = Nothing
    End Sub
    Last edited by T-J; 02-20-2008 at 06:58 PM. Reason: Original post moved to new thread

  3. #3
    Registered User
    Join Date
    02-20-2008
    Posts
    2
    Many Thanks. I'll give it a try.

+ 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