merge multiple files from text list of file pairs

  • Thread starter Thread starter rroach
  • Start date Start date
R

rroach

I am looking for some examples to get me going on a project to read a
text file which contains in column a and column b file name pairs. I
want to open the two files referred to in a1 and b1, copy a range from
file b1 into a set location in file a1, save a1, close both, then loop
to files a2 and b2 etc. Have about 1000 pairs to loop through so
investing time into getting this to work in an automated fashion would
be great.

Anyone have some examples or ideas regarding the toughest part which to
me is getting the file name pairs, copying code from b to a?

Thanks.

Rob
 
Hi Rob,

Try something like:

Sub Tester()

Dim wbList As Workbook
Dim wbSrc As Workbook
Dim wbDest As Workbook
Dim rcell As Range
Const copyAddress As String = "F1:F4"
Const destAddress As String = "A1"

With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With

Set wbList = Workbooks.Open("YourFilelistBook")

For Each rcell In wbList.Sheets(1).Range("A1"). _
CurrentRegion.Columns(1).Cells
Set wbDest = Workbooks.Open(rcell.Value)
Set wbSrc = Workbooks.Open(rcell(1, 2).Value)
wbSrc.Sheets(1).Range(copyAddress).Copy _
Destination:=wbDest.Sheets(1).Range(destAddress)
wbSrc.Close savechanges:=False
wbDest.Close savechanges:=True
Set wbSrc = Nothing
Set wbDest = Nothing
Next

wbList.Close savechanges:=False

With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With

End Sub
 
Norman,

Wow! Nice solution. Thanks so much!

I'll let you know how it goes, but after reading and glimpsing its
simplicity I think it will work just fine.

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top