+ Reply to Thread
Results 1 to 12 of 12

Copying data from other workbooks and pasting into master workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    07-05-2013
    Location
    South Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Question Copying data from other workbooks and pasting into master workbook

    OK so I have tried everything I can think of to modify this macro I pulled from the net but I keep getting errors. I don’t know where I am going wrong in addition to my limited knowledge of coding macros has really put me in a bind. I need this macro to prompt me to select a workbook(s) then copy everything on Sheet2 to my master workbook until all of the source workbooks have been processed into the master.

    Here is my code. I have reverted it back to the original I copied from the net because I butchered the original too much.
    I have also included my sample data.

    Sub Collect_Data()
    
      Dim C As Long
      Dim DstWks1 As Worksheet
      Dim DstWks2 As Worksheet
      Dim LastRow As Long
      Dim R As Long
      Dim SrcWkb As Workbook
      Dim StartRow As Long
      Dim wkbname As Variant
      Dim xlsFiles As Variant
    
      
       'Starting column and row for the destination workbook
        C = 1
        R = 1
       'Set references to destination workbook worksheet objects
        Set DstWks1 = ThisWorkbook.Worksheets("Sheet1")
        Set DstWks2 = ThisWorkbook.Worksheets("Sheet2")
        
       'Starting row on source worksheet
        StartRow = 1
        
       'Get the workbooks to open GetOpenFilename
        xlsFiles = Application.FileDialog(FileFilter:="Excel files (*.xls*),*.xls*", MultiSelect:=True) 'Excel files (*.xls),
        Application.AskToUpdateLinks = False
          If VarType(xlsFiles) = vbBoolean Then Exit Sub
          
         'Loop through each workbook and copy the data to this workbook
          For Each wkbname In xlsFiles
            Set SrcWkb = Workbooks.Open(Filename:=wkbname, ReadOnly:=True)
              LastRow = SrcWkb.Worksheets(2).Cells(Rows.Count, "AB").End(xlUp).Row
                Cells(Rows.Count, "AD").Select
                Selection.Copy
                If LastRow >= StartRow Then
                  With SrcWkb.Worksheets(2)
                    DstWks1.Cells(R, C).Resize(LastRow - StartRow + 1, 1).Value = _
                    .Range(.Cells(StartRow, "AD"), .Cells(LastRow, "AD")).Value
                    Sheets(1).Select
                    ActiveSheet.Paste
                  End With
                End If
              LastRow = SrcWkb.Worksheets(1).Cells(Rows.Count, "AD").End(xlUp).Row
                If LastRow >= StartRow Then
                  With SrcWkb.Worksheets(1)
                    DstWks2.Cells(R, C).Resize(LastRow - StartRow + 1, 1).Value = _
                    .Range(.Cells(StartRow, "AD"), .Cells(LastRow, "AD")).Value
                  End With
                End If
            C = C + 1
            SrcWkb.Close SaveChanges:=False
            ''SrcWkb.Close''
          Next wkbname
          
    End Sub
    UNSPSC_v14_0801.xlsUNSPSC_v15_1101.xlsMaster.xlsx

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    re: Copying data from other workbooks and pasting into master workbook

    You did not specify what problems you encountered with the code. Did you get any error? If so, which line of the code? If there is no error, what is not working?
    You are coping from each book the second sheet in to sheet1, but if the index of sheets have changed, the code may copy the wrong data
    Last edited by AB33; 07-14-2013 at 04:56 PM.

  3. #3
    Registered User
    Join Date
    07-05-2013
    Location
    South Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Copying data from other workbooks and pasting into master workbook

    Nope not error, The macro sems to let me select the file, then the screen flashes, and the macro ends. It never goes past this line:
    I got it past this line once but it thew an out of range error. Sorry I didnt make note of the actual error code.

    xlsFiles = Application.GetOpenFilename(FileFilter:="Excel files (*.xls*),*.xls*", MultiSelect:=True) 'Excel files (*.xls),

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    re: Copying data from other workbooks and pasting into master workbook

    First correction

     xlsFiles = Application.GetOpenFilename(FileFilter:="Excel files (*.xls*),*.xls*", MultiSelect:=True) 'Excel files (*.xls),

  5. #5
    Registered User
    Join Date
    07-05-2013
    Location
    South Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Copying data from other workbooks and pasting into master workbook

    @ AB33 - Both my original line and yours seemed function properly but nothing actual goes anywhere after that. Just terminates the macro.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copying data from other workbooks and pasting into master workbook

    Well, There are few issues with the code.
    You said you want to copy everything, yet
    Cells(Rows.Count, "AD").Select
    selects all rows in column AD only. You have also few other lines which may either be unnecessary or could be written differently.
    What column/ rows do you want to copy? Do you have sheets called sheet1 and sheet2 in your data?
    Last edited by AB33; 07-14-2013 at 05:13 PM.

  7. #7
    Registered User
    Join Date
    07-05-2013
    Location
    South Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Copying data from other workbooks and pasting into master workbook

    I meant A through D. Dang it. ok so I placed a : between all the ranges and I now get a Run-Time Error 1004 at this line:
    LastRow = SrcWkb.Worksheets(2).Cells(Rows.Count, "A:D").End(xlUp).Row

    The sheet names in the master file are Sheet1, Sheet2, Sheet3 but the source file names are custom per tab. I want everything off of tab two Columns A:D. Which parts of the code do you feel is unnecessary? My coding experience is REALLY rusty.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copying data from other workbooks and pasting into master workbook

      LastRow = SrcWkb.Worksheets(2).Cells(Rows.Count, "A").End(xlUp).Row
    OR
     LastRow = SrcWkb.Worksheets(2).Cells(Rows.Count, "D").End(xlUp).Row

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copying data from other workbooks and pasting into master workbook

    I will be off site until tomorrow.

  10. #10
    Registered User
    Join Date
    07-05-2013
    Location
    South Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Copying data from other workbooks and pasting into master workbook

    Lastrow Should be "D". Here is what I have now and it does run through the sheets but it doesnot paste the values in the master fille.

    Sub Collect_Data()
    
      Dim C As Long
      Dim DstWks1 As Worksheet
      Dim DstWks2 As Worksheet
      Dim LastRow As Long
      Dim R As Long
      Dim SrcWkb As Workbook
      Dim StartRow As Long
      Dim wkbname As Variant
      Dim xlsFiles As Variant
    
      
       'Starting column and row for the destination workbook
        C = 1
        R = 1
       'Set references to destination workbook worksheet objects
        Set DstWks1 = ThisWorkbook.Worksheets("Sheet1")
        Set DstWks2 = ThisWorkbook.Worksheets("Sheet2")
        
       'Starting row on source worksheet
        StartRow = 1
        
       'Get the workbooks to open GetOpenFilename
        'xlsFiles = Application.FileDialog(FileFilter:="Excel files (*.xls*),*.xls*", MultiSelect:=True) 'Excel files (*.xls),
        xlsFiles = Application.GetOpenFilename(FileFilter:="Excel files (*.xls*),*.xls*", MultiSelect:=True) 'Excel files (*.xls),
        Application.AskToUpdateLinks = False
          If VarType(xlsFiles) = vbBoolean Then Exit Sub
          
         'Loop through each workbook and copy the data to this workbook
          For Each wkbname In xlsFiles
            Set SrcWkb = Workbooks.Open(Filename:=wkbname, ReadOnly:=True)
              LastRow = SrcWkb.Worksheets(2).Cells(Rows.Count, "D").End(xlUp).Row
                Cells(Rows.Count, "D").Select
                Selection.Copy
                If LastRow >= StartRow Then
                  With SrcWkb.Worksheets(2)
                    DstWks1.Cells(R, C).Resize(LastRow - StartRow + 1, 1).Value = _
                    .Range(.Cells(StartRow, "A"), .Cells(LastRow, "D")).Value
                    Sheets(1).Select
                    ActiveSheet.Paste
                  End With
                End If
              LastRow = SrcWkb.Worksheets(1).Cells(Rows.Count, "D").End(xlUp).Row
                If LastRow >= StartRow Then
                  With SrcWkb.Worksheets(1)
                    DstWks2.Cells(R, C).Resize(LastRow - StartRow + 1, 1).Value = _
                    .Range(.Cells(StartRow, "A"), .Cells(LastRow, "D")).Value
                  End With
                End If
            C = C + 1
            SrcWkb.Close SaveChanges:=False
            ''SrcWkb.Close''
          Next wkbname
          
    End Sub

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copying data from other workbooks and pasting into master workbook

    This is my guess work on what you are trying to do.
    You should have master book with sheet1 as a destination sheet. Sheet1 has also a header.

    Sub Collect_Data()
    
      Dim DstWks1 As Worksheet, DstWks2 As Worksheet, LastRow As Long, NR As Long, SrcWkb As Workbook, StartRow As Long
      Dim wkbname As Variant
      Dim xlsFiles As Variant
      Application.ScreenUpdating = 0
    
       'Set references to destination workbook worksheet objects
        Set DstWks1 = ThisWorkbook.Worksheets("Sheet1")
        Set DstWks2 = ThisWorkbook.Worksheets("Sheet2")
    
       'Starting row on source worksheet
        StartRow = 1
        
       'Get the workbooks to open GetOpenFilename
       
        xlsFiles = Application.GetOpenFilename(FileFilter:="Excel files (*.xls*),*.xls*", MultiSelect:=True) 'Excel files (*.xls),
        Application.AskToUpdateLinks = False
          If VarType(xlsFiles) = vbBoolean Then Exit Sub
          
         'Loop through each workbook and copy the data to this workbook
          For Each wkbname In xlsFiles
            Set SrcWkb = Workbooks.Open(fileName:=wkbname, ReadOnly:=True)
            With SrcWkb.Worksheets(2)
                LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
                .Range("A2:D" & LastRow).Copy
                NR = DstWks1.Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
                DstWks1.Range("A" & NR).PasteSpecial xlValues
            End With
             Application.ScreenUpdating = True
            SrcWkb.Close SaveChanges:=False
            ''SrcWkb.Close''
          Next wkbname
          
    End Sub

  12. #12
    Registered User
    Join Date
    07-05-2013
    Location
    South Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Copying data from other workbooks and pasting into master workbook

    AB33 - Thats exactly what i am looking for. Thank your for your help. You are a life saver!

+ 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] Macro for Copying Cels from All Workbooks to a Single Master Workbook
    By JohnnyJ2013 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-25-2013, 05:51 AM
  2. Conditioned copying/pasting of specific cells from multiple workbooks to master workbook
    By Ziad Homaidan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-06-2013, 11:39 AM
  3. Replies: 1
    Last Post: 11-21-2012, 11:23 AM
  4. Replies: 1
    Last Post: 07-20-2012, 06:20 PM
  5. Pasting data from MANY workbooks into specific worksheets of a master workbook
    By beemoney19 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-17-2012, 11:30 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