+ Reply to Thread
Results 1 to 10 of 10

How to add the columns data of several xlsx files of a folder in another xlsx file

Hybrid View

  1. #1
    Registered User
    Join Date
    05-21-2012
    Location
    india
    MS-Off Ver
    Excel 2010
    Posts
    75

    Post How to add the columns data of several xlsx files of a folder in another xlsx file

    Hi All,

    If any one knows the solution to this thread please help me.

    I have a folder with no.of .xlsx files the structure of all the files will be same
    eg:
    ID Category Name
    1 Mobiles Samsung
    2 Laptops Dell
    3 Desktops Acer

    Like the above the data will be available in other files also and the ID should be auto increment column.Now i need to add the data along with labels in other excel file.

    Please help me and this is an urgent need to me.

    Regards
    Rav

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

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    You mean you want to consolidate the data of all the files into one separate workbook?
    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
    05-21-2012
    Location
    india
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    Hi Arlette,

    Thanks for the response

    Yes that is what exactly i want.

    Regards
    Rav

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

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    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
    
    '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 & "\*.xlsx")
    
    'Create a workbook for the recap report
    Set Master = ThisWorkbook
    
    With Master.Worksheets("Sheet1")
        .Range("A1").Value = "ID"
        .Range("B1").Value = "Category"
        .Range("C1").Value = "Name"
    End With
    
    Do
        Workbooks.Open (myPath & "\" & CurrentFileName)
        Set sourceBook = Workbooks(CurrentFileName)
        Set sourceData = sourceBook.Worksheets("Sheet1")
        
            With sourceData
                .Range("A2:C" & Range("C" & Rows.Count).End(xlUp).Row).Copy _
                        Master.Worksheets("Sheet1").Range("A" & Master.Worksheets(1).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 <> ""
    
    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.

  5. #5
    Registered User
    Join Date
    05-21-2012
    Location
    india
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    Arlette,

    Yes it is working

    but The ID is needs to be an auto increment data column(this will not satisfy)

    Regards
    Rav

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

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    Made a few changes.
    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 & "\*.xlsx")
    
    'Create a workbook for the recap report
    Set Master = ThisWorkbook
    
    With Master.Worksheets("Sheet1")
        .Range("A1").Value = "ID"
        .Range("B1").Value = "Category"
        .Range("C1").Value = "Name"
    End With
    
    Do
        Workbooks.Open (myPath & "\" & CurrentFileName)
        Set sourceBook = Workbooks(CurrentFileName)
        Set sourceData = sourceBook.Worksheets("Sheet1")
        
            With sourceData
                .Range("B2:C" & Range("C" & .Rows.Count).End(xlUp).Row).Copy _
                        Master.Worksheets("Sheet1").Range("B" & Master.Worksheets(1).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 <> ""
    
    With Master.Worksheets("Sheet1")
        .Range("A2:A" & Range("B" & .Rows.Count).End(xlUp).Row).ClearContents
        .Range("A2:A" & Range("B" & .Rows.Count).End(xlUp).Row).FormulaR1C1 = "=row()-1"
        .Range("A2:A" & Range("B" & .Rows.Count).End(xlUp).Row).Value = .Range("A2:A" & Range("B" & .Rows.Count).End(xlUp).Row).Value
    End With
    
    Application.ScreenUpdating = True
    
    End Sub

  7. #7
    Registered User
    Join Date
    05-21-2012
    Location
    india
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    Alrette,

    See this is how i'm getting


    ID Category Name
    2 1
    ID 2 3
    3 1 3
    4 2 2
    5 1 2
    6 2 1
    7 2 3
    8 1 3
    9 2 2
    10 1 2
    11 2 1
    12 2 3
    13 1 3
    14 2 2
    15 1 2
    16 2 1
    17 2 3
    18 1 3
    19 2 2
    20 1 2

    and one more thing is if i run the macro each time the same data from those files is repeatedly adding.I want to avoid this


    Regards
    Rav

  8. #8
    Registered User
    Join Date
    05-21-2012
    Location
    india
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    Arette,

    I am getting now by replacing the
    .Range("A2:A" & Range("B" & .Rows.Count).End(xlUp).Row).FormulaR1C1 = "=row()-1" with
    .Range("A2:A" & Range("B" & .Rows.Count).End(xlUp).Row).FormulaR1C1 = "=row()-3"

    But in my previous post i am facing the problem like repeatably adding the data

    Can we do like clearing the existed data and adding it again then any time it shows the actual data.
    or in any other way

    Regards
    Rav

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

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    Are your headers not in row1 in the final output file? In that case, your change from -1 to-3 is fine.

    Change this code
     With Master.Worksheets("Sheet1")
        .Range("A1").Value = "ID"
        .Range("B1").Value = "Category"
        .Range("C1").Value = "Name"
    End With
    to this
    With Master.Worksheets("Sheet1")
        .cells.clearcontents
        .Range("A1").Value = "ID"
        .Range("B1").Value = "Category"
        .Range("C1").Value = "Name"
    End With

  10. #10
    Registered User
    Join Date
    05-21-2012
    Location
    india
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: How to add the columns data of several xlsx files of a folder in another xlsx file

    Hi Arlu,

    Sorry for disturbing you once again on this one.

    Actually With your code i am able to consolidate all the excel files data in a folder in another excel file.

    Here all the excel files columns data are having data validation control of list.So, that when i am consolidating the data came along with those data validation list controls.
    Here the data may change accidentally/Forcefully.I need to avoid those controls when copying and just data i need.

    One more thing is how to avoid the user without editing those data.

    Hope i explained well and you understand the problems too

    Please help me as early as possible.it's urgent.

    Regards
    Rav

+ 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