+ Reply to Thread
Results 1 to 14 of 14

Copy certain columns from all the files and paste in a Summary workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    31

    Thumbs up Copy certain columns from all the files and paste in a Summary workbook

    Hi Team,

    I have around 20 files in a drive and a summary workbook. All that I would like to achieve is, from all the files, I want column (B, C, N, O, Z, AP, AQ) come into summary workbook one below another. All these column has to be placed in same columns in summary workbook as well.

    Here, I want macro to copy all the specified columns from row 2 since row 1 has a heading and paste in row 3 in summary workbook since row 1 and 2 has a merged heading. Also, I would like to retain the values, formats, comments if any and validations.


    Please advice is it possible. Any help that you could provide on this would be really appreciated.


    Thanks,
    Vignesh

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Yes its possible.

    Do you want the name of the file from where the data was copied?

    I believe this macro will need to reside in the summary file and i assume that the header will already be present. So i would only need to help you populate the rest of the data, right?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Search for "Looping through workbooks in a folder" to get you started.

  4. #4
    Registered User
    Join Date
    09-21-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    31

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Hi Arul,

    I dont require the file names to be copied.

    Also, in the source files, the header is in first row and in the summary file the header present in first two rows.

    The rest of the data i want to copy from all the source files from line 2 and paste in summary sheet from line 3.

    Please advice....


    Thanks,
    Vignesh

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Try this code
    Option Explicit
    Sub cons_data()
    
    Dim Master As Workbook
    Dim sourceBook As Workbook
    Dim sourceData As Worksheet
    Dim CurrentFileName As String
    Dim myPath As String
    
    Application.ScreenUpdating = False
    
    'The folder containing the files to be recap'd
    myPath = "D:\Test"
    
    'Finds the name of the first file of type .xls in the current directory
    CurrentFileName = Dir(myPath & "\*.xls")
    
    'Create a workbook for the recap report
    Set Master = ThisWorkbook
    
    Do
        Workbooks.Open (myPath & "\" & CurrentFileName)
        Set sourceBook = Workbooks(CurrentFileName)
        Set sourceData = sourceBook.Worksheets(1)
        
            With sourceData
                lrow = .Range("B" & .Rows.Count).End(xlUp).Row
                .Range("B2:C" & lrow).Copy Master.Worksheets(1).Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
                .Range("N2:O" & lrow).Copy Master.Worksheets(1).Range("N" & Rows.Count).End(xlUp).Offset(1, 0)
                .Range("Z2:Z" & lrow).Copy Master.Worksheets(1).Range("Z" & Rows.Count).End(xlUp).Offset(1, 0)
                .Range("AP2:AQ" & lrow).Copy Master.Worksheets(1).Range("AP" & Rows.Count).End(xlUp).Offset(1, 0)
            End With
           
        sourceBook.Close
      
    'Calling DIR w/o argument finds the next .xlsx file within the current directory.
    CurrentFileName = Dir()
    Loop While CurrentFileName <> ""
    
    MsgBox "Consolidation complete"
    
    Application.ScreenUpdating = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button

  6. #6
    Registered User
    Join Date
    09-21-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    31

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Hi Arul,

    Thanks a lot. It works like charm but is there anyway that it can be done for .xlsx files instead of .xls.

    Sorry for not specifying this earlier.


    Thanks,
    Vignesh

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Change
    CurrentFileName = Dir(myPath & "\*.xls")
    to
    CurrentFileName = Dir(myPath & "\*.xlsx")

  8. #8
    Registered User
    Join Date
    09-21-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    31

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Sorry for the delay reply... Thanks a lot for the help

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Np ...If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  10. #10
    Registered User
    Join Date
    09-21-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    31

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Hi Arul,

    I am sorry, In the above code, I am not getting any error but I do not see any of the columns are getting copied and pasted. Also, I am getting an error in the line:

    lrow = .Range("B" & .Rows.Count).End(xlUp).Row
    However I have tried declaring a variable stating "Dim Lrow As Long" but still it was not working.

    Could you please help me.... thanks!!!

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy certain columns from all the files and paste in a Summary workbook

    What error are you getting?

  12. #12
    Registered User
    Join Date
    09-21-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    31

    Re: Copy certain columns from all the files and paste in a Summary workbook

    I am not getting any error if i declare the variable stating "Dim Lrow As Long" but the columns which we are specifying are not getting copied.

    If i do not mention the variable, I am getting an error in the below line stating:"Compile Error, Variable not defined"

    lrow = .Range("B" & .Rows.Count).End(xlUp).Row

  13. #13
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Ok, yes, you have to declare dim lrow as long.

    Can you attach the file or a sample of what you are using?

  14. #14
    Registered User
    Join Date
    09-21-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    31

    Re: Copy certain columns from all the files and paste in a Summary workbook

    Sorry Arlette, its my mistake.... I have moved the sheet to 3rd but haven't changed the destination

    Thanks a lot again

+ 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