Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wonder whether someone may be able to help me please.

I'm using the code below to allow a user to merge multiple workbooks from different folders into one 'Summary' worksheet.

Sub Merge()

      Dim DestWB As Workbook, WB As Workbook, WS As Worksheet, SourceSheet As String
      Set DestWB = ActiveWorkbook
      SourceSheet = "Input"
      startrow = 7
      FileNames = Application.GetOpenFilename( _
      filefilter:="Excel Files (*.xls*),*.xls*", _
      Title:="Select the workbooks to merge.", MultiSelect:=True)
      If IsArray(FileNames) = False Then
          If FileNames = False Then
              Exit Sub
          End If
      End If
      For n = LBound(FileNames) To UBound(FileNames)
          Set WB = Workbooks.Open(Filename:=FileNames(n), ReadOnly:=True)
          For Each WS In WB.Worksheets
              If WS.Name = SourceSheet Then
                  With WS
                      If .UsedRange.Cells.Count > 1 Then
                          dr = DestWB.Worksheets("Input").Range("C" & DestWB.Worksheets("Input").Rows.Count).End(xlUp).Row + 1
                          Lastrow = .Range("C" & Rows.Count).End(xlUp).Row
                          If Lastrow >= startrow Then
                              .Range("A" & startrow & ":AE" & Lastrow).Copy
                              DestWB.Worksheets("Input").Cells(dr, "A").PasteSpecial xlValues
                          End If
                      End If
                  End With
                  Exit For
              End If
          Next WS
          WB.Close savechanges:=False
      Next n
  End Sub

The code works, but I'd like to tweak this a little, so that instead of the user having to manually select the files to merge, the macro reads a list of the file names and file paths and automatically copies and pastes the relevant data into the "Summary" sheet.

I've set my Summary workbook up so there is a sheet called "Lists" with the file names listed in B3:B10 and in C3:C10 the associated file paths. I've searched "Stackoverflow" and carried out Google searches, and although I've found a number of posts on this topic, they don't show me how to read from a list of filenames and paths, but rather hard coding a specific directory which doesn't suit my needs.

I just wondered whether someone could possibly look at this please and offer some guidance on how I may be able to acheive this.

Many thanks and kind regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
712 views
Welcome To Ask or Share your Answers For Others

1 Answer

Kindly use the addin RDBMerge.

RDBMerge is a user friendly way to Merge Data from Multiple Excel Workbooks, csv and xml files into a Summary Workbook

http://www.rondebruin.nl/merge.htm


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...