+ Reply to Thread
Results 1 to 18 of 18

Macro copy data from multiple workbooks to master

Hybrid View

pram Macro copy data from multiple... 02-28-2013, 05:18 PM
arlu1201 Re: Macro copy data from... 03-01-2013, 01:54 AM
pram Re: Macro copy data from... 03-07-2013, 04:43 PM
arlu1201 Re: Macro copy data from... 03-08-2013, 10:43 AM
pram Re: Macro copy data from... 03-08-2013, 03:31 PM
arlu1201 Re: Macro copy data from... 03-11-2013, 06:50 AM
pram Re: Macro copy data from... 03-11-2013, 03:50 PM
arlu1201 Re: Macro copy data from... 03-11-2013, 03:55 PM
pram Re: Macro copy data from... 03-11-2013, 04:04 PM
arlu1201 Re: Macro copy data from... 03-12-2013, 01:31 AM
pram Re: Macro copy data from... 03-12-2013, 02:29 PM
pram Re: Macro copy data from... 03-13-2013, 03:58 PM
arlu1201 Re: Macro copy data from... 03-13-2013, 06:43 AM
arlu1201 Re: Macro copy data from... 03-14-2013, 03:41 AM
pram Re: Macro copy data from... 03-14-2013, 02:45 PM
arlu1201 Re: Macro copy data from... 03-15-2013, 01:16 AM
pram Re: Macro copy data from... 03-15-2013, 03:08 PM
arlu1201 Re: Macro copy data from... 03-15-2013, 03:14 PM
  1. #1
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Macro copy data from multiple workbooks to master

    Hi Arlu,
    i am getting same error code 9 but unble to resolve the issue can you please confirm where i nedd to change in above code.
    and how can i save data directly in different drive.

  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: Macro copy data from multiple workbooks to master sheet

    In this code - change the bold sections to match your system -
    Option Explicit
    
    Sub cons_files()
    
    Dim Master As Workbook
    Dim sourceBook As Workbook
    Dim sourceData As Worksheet
    Dim CurrentFileName As String
    Dim myPath As String
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    'The folder containing the files to be recap'd
    myPath = "B:\Test"
    
    'Finds the name of the first file of type .xls in the current directory
    CurrentFileName = Dir(myPath & "\*.xlsx")
    
    'Create a workbook for the recap report
    Set Master = ThisWorkbook
    
    Do
        Workbooks.Open (myPath & "\" & CurrentFileName)
        Set sourceBook = Workbooks(CurrentFileName)
        Set sourceData = sourceBook.Worksheets("MASTER")
        
            With sourceData
                .Range("A2:O" & Range("A" & Rows.Count).End(xlUp).Row).Copy _
                        Master.Worksheets("MASTER").Range("A" & 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
    Application.DisplayAlerts = True
    
    End Sub
    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
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master sheet

    Hi Arlu,

    Thanks for your quick reply.
    I tried to use your code but i didn't get what i want or unble to use it.

    i want to say i am not too much proficient in VB so it my humble request to you to go through attached file.

    in this file i want to consolidate individual scores(score& score 1) in total score excell sheet lets assume score file is saved in c drive and total score is saved in d drive.
    How can i do this.
    Your help will be too much appreciated.
    Thanks
    pram
    Attached Files Attached Files

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

    Re: Macro copy data from multiple workbooks to master

    If there is any data in the final score sheet, do you want it to be cleared before fresh data is input?

  5. #5
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master

    Hi Arlu,
    Thanks for your reply.
    i don't want to clear previous data, i want to save fresh data every time with every workbook in different cells.

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

    Re: Macro copy data from multiple workbooks to master

    Try this code - save it in the Final score file as per the instructions given below the code - change the highlighted sections to match your system -
    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 = "B:\Test"
    
    'Finds the name of the first file of type .xls in the current directory
    CurrentFileName = Dir(myPath & "\*.xlsx")
    
    '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
                .Range("A2:B2").Copy Master.Worksheets(1).Range("A" & 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 "Done"
    
    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 Tools | Macro | Macros
    Select a macro in the list, and click the Run button

    Note: Your profile says you are using excel 2003 but the files you uploaded are of excel 2007 and above. Please change your profile info to reflect that.

  7. #7
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master

    Hi Arlu,
    Thanks for your quick responce but
    I am geting compilation error msg 9 at bold section can u please check and reply me as i am seeing u r online this time.

    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 = "C:\Score"
    
    'Finds the name of the first file of type .xls in the current directory
    CurrentFileName = Dir(myPath & "\*.xlsx")
    
    '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
                .Range("A2:B2").Copy Master.Worksheets(1).Range("A" & 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 "Done"
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by arlu1201; 03-11-2013 at 03:54 PM.

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

    Re: Macro copy data from multiple workbooks to master

    Firstly,

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. If you need more information on how to use them, check my signature below this post.

    Have you changed this line CurrentFileName = Dir(myPath & "\*.xlsx") in your code?

  9. #9
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master

    Hi Arlu,
    Sorry for my mistake, i am new on this forum.
    Yes i have already changed it but still i am getting error if you will change the coding in my attached excel sheet it will be very beneficial for me.

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

    Re: Macro copy data from multiple workbooks to master

    You do not need to change this line CurrentFileName = Dir(myPath & "\*.xlsx").

    The only line you need to change is myPath = "B:\Test"

  11. #11
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master

    Hi Arlu,
    i am getting same error msg when i am running this macro its open only score excel file.
    after that i get error msg 9

  12. #12
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master

    Set sourceBook = Workbooks(CurrentFileName)

  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: Macro copy data from multiple workbooks to master

    Error msg 9 on which line?

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

    Re: Macro copy data from multiple workbooks to master

    Replace this line
    Workbooks.Open (myPath & "" & CurrentFileName)
    with
    Workbooks.Open (myPath & "\" & CurrentFileName)

  15. #15
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master

    Hi Arlu,
    Now i am getting other error msg.
    Please see attached image file.
    Attached Images Attached Images

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

    Re: Macro copy data from multiple workbooks to master

    That error shows that you do not have any .xlsx files in your folder.

    If your files are 2003 version files, then change the .xlsx to .xls in that code line.

  17. #17
    Registered User
    Join Date
    07-26-2012
    Location
    delhi
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro copy data from multiple workbooks to master

    Hi Arlu,
    I am using office 10 if you want any help please see my previous attached excelsheet.

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

    Re: Macro copy data from multiple workbooks to master

    Change your profile details to reflect excel 2010.

    There is no problem with your file. Have you cross checked that the path in the macro matches your system where the files exist?

+ 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