+ Reply to Thread
Results 1 to 4 of 4

Merge Selected Workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    4

    Merge Selected Workbooks

    Hi,

    I got a Macro code to merge data from different Excel files from the forum. I was trying to modify Macro code to extract data from different cells from a file to destination file. I want data from B2, B4, B8 and column E2:E12 to be copied and paste it to the destination work book by transposing and concatenate E2:E12 and separate with comma(,) from each file. I am failing to modify to suit my requirements. Can any body help me please.


    Sub MergeSelectedWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim SelectedFiles() As Variant
    Dim NRow As Long
    Dim FileName As String
    Dim NFile As Long
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range

    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)

    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Documents and Settings\admin\Desktop\Results_4"

    ' Set the current directory to the the folder path.
    ChDrive FolderPath
    ChDir FolderPath

    ' Open the file dialog box and filter on Excel files, allowing multiple files
    ' to be selected.
    SelectedFiles = Application.GetOpenFilename( _
    filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)

    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 1

    ' Loop through the list of returned file names
    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
    ' Set FileName to be the current workbook file name to open.
    FileName = SelectedFiles(NFile)

    ' Open the current workbook.
    Set WorkBk = Workbooks.Open(FileName)

    ' Set the cell in column A to be the file name.
    SummarySheet.Range("A" & NRow).Value = FileName

    ' Set the source range to be A9 through C9.
    ' Modify this range for your workbooks. It can span multiple rows.
    Set SourceRange = WorkBk.Worksheets(1).Range("A10:B10")


    ' Set the destination range to start at column B and be the same size as the source range.
    Set DestRange = SummarySheet.Range("B" & NRow)
    Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
    SourceRange.Columns.Count)

    ' Copy over the values from the source to the destination.
    DestRange.Value = SourceRange.Value

    ' Increase NRow so that we know where to copy data next.
    NRow = NRow + DestRange.Rows.Count

    ' Close the source workbook without saving changes.
    WorkBk.Close savechanges:=False
    Next NFile
    ' Call AutoFit on the destination sheet so that all data is readable.
    SummarySheet.Columns.AutoFit
    End Sub

    Regards
    Prakash

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Merge Selected Workbooks

    HI Prakash

    could u plz provide us the sample workbook

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Merge Selected Workbooks

    Hi, Prakash,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here


    untested
    Sub MergeSelectedWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim SelectedFiles() As Variant
    Dim NRow As Long
    Dim FileName As String
    Dim NFile As Long
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
    Dim varCells As Variant
    Dim lngArr As Long
    Dim lngToGether As Long
    Dim strText As String
    
    varCells = Array("B2", "B4", "B8")
    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Documents and Settings\admin\Desktop\Results_4"
    
    ' Set the current directory to the the folder path.
    ChDrive Left(FolderPath, 1)
    ChDir FolderPath
    
    ' Open the file dialog box and filter on Excel files, allowing multiple files
    ' to be selected.
    SelectedFiles = Application.GetOpenFilename( _
    filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
    
    ' NRow keeps track of where to insert new rows in the destination workbook.
    'NRow = 1
    
    ' Loop through the list of returned file names
    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
      ' Set FileName to be the current workbook file name to open.
      FileName = SelectedFiles(NFile)
      
      ' Open the current workbook.
      Set WorkBk = Workbooks.Open(FileName)
      
      NRow = SummarySheet.Range("A" & Rows.Count).End(xlUp).Row
      ' Set the cell in column A to be the file name.
      SummarySheet.Range("A" & NRow).Value = FileName
      For lngArr = LBound(varCells) To UBound(varCells)
        SummarySheet.Cells(NRow, 2 + lngArr).Value = WorkBk.Worksheets(1).Range(varCells(lngArr)).Value
      Next lngArr
      For lngToGether = 2 To 10
        strText = strText & WorkBk.Worksheets(1).Cells(lngToGether, "E").Value & ", "
      Next lngToGether
      SummarySheet.Cells(NRow, 5).Value = Left(strText, Len(strText) - 2)
    '  ' Set the source range to be A9 through C9.
    '  ' Modify this range for your workbooks. It can span multiple rows.
    '  Set SourceRange = WorkBk.Worksheets(1).Range("A10:B10")
    '
    '
    '  ' Set the destination range to start at column B and be the same size as the source range.
    '  Set DestRange = SummarySheet.Range("B" & NRow)
    '  Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
    '      SourceRange.Columns.Count)
    '
    '  ' Copy over the values from the source to the destination.
    '  DestRange.Value = SourceRange.Value
      
      ' Increase NRow so that we know where to copy data next.
      
      ' Close the source workbook without saving changes.
      WorkBk.Close savechanges:=False
    Next NFile
    ' Call AutoFit on the destination sheet so that all data is readable.
    SummarySheet.Columns.AutoFit
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    05-31-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Merge Selected Workbooks

    Thanks HaHoBe. I shall take care of the rule of the forum.

    I think I was not clear in my question.

    The above posted code works perfectly for me.It is extracting data only from A10:B10. The folder is having nearly 100 files and code should read each file and get data from B1, B3,B10 and E2:E11. In the destination file B1,B3,B4 should be written in different column and data in E2:E11 should be concatenated written to another column. Then the next file data should be written in a next row. Like wise once it encounters the end of file it should stop.

    Hope I have made it clear.

+ 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