The code below is for merging 3 separate excel files into 1 file. It works for Excel 2007 but does not work with Excel 2016. The code below is written for Visual Foxpro but you should understand it. Are there any changes in Excel 2016 VBA I should be aware of that stops the code from working. With Excel 2016 data is not copied into tabs 2 and 3 but the tabs are created.
IF VARTYPE(loXL) = [O]
lcfrom1='d:\excel1.xlsb'
lcfrom2='d:\excel2.xlsb'
loXL=GETOBJECT(',','Excel.Application')
**Open To file (file to merged into)
lcto='d:\excel5.xlsb'
loWBto=loXL.workbooks.open(lcto)
lowbto.Sheets('tab1').Move(,lowbto.Sheets(1))
lowbto.sheets('Tab1').activate
***Merge file 1 - excel1
loWBfrom=loxl.workbooks.open(lcFrom1)
lowbto.sheets.add.name="Tab2"
lowbto.Sheets('tab2').Move(,lowbto.Sheets(2))
lowbfrom.sheets(1).activate
loxl.cells.select
loxl.selection.copy
lowbto.sheets('tab2').activate
loxl.Selection.pastespecial &&loxl.Selection.pastespecial(-4104,-4142,.f.,.f.)
loxl.range("A1:A1").select
lowbfrom.application.cutcopymode = .f.
loWBfrom.close(0)
***Merge file 2 - excel2
loWBfrom=loxl.workbooks.open(lcFrom2)
lowbto.sheets.add.name="Tab3"
lowbto.Sheets('Tab3').Move(,lowbto.Sheets(3))
lowbfrom.sheets(1).activate
loxl.cells.select
loxl.selection.copy
lowbto.sheets('Tab3').activate
loxl.Selection.pastespecial
loxl.range("A1:A1").select
lowbfrom.application.cutcopymode = .f.
loWBfrom.close(0)
lowbto.sheets('Tab1').activate
loxl.range("A1:A1").select
lowbto.sheets(1).activate
lowbto.save
loXL.visible=.t.
ELSE
=MESSAGEBOX('Error Opening Excel. Cannot create XLS report',16,'Error Message')
ENDIF
IF VARTYPE(loXL) = [O]
lcfrom1='d:\excel1.xlsb'
lcfrom2='d:\excel2.xlsb'
loXL=GETOBJECT(',','Excel.Application')
**Open To file (file to merged into)
lcto='d:\excel5.xlsb'
loWBto=loXL.workbooks.open(lcto)
lowbto.Sheets('tab1').Move(,lowbto.Sheets(1))
lowbto.sheets('Tab1').activate
***Merge file 1 - excel1
loWBfrom=loxl.workbooks.open(lcFrom1)
lowbto.sheets.add.name="Tab2"
lowbto.Sheets('tab2').Move(,lowbto.Sheets(2))
lowbfrom.sheets(1).activate
loxl.cells.select
loxl.selection.copy
lowbto.sheets('tab2').activate
loxl.Selection.pastespecial &&loxl.Selection.pastespecial(-4104,-4142,.f.,.f.)
loxl.range("A1:A1").select
lowbfrom.application.cutcopymode = .f.
loWBfrom.close(0)
***Merge file 2 - excel2
loWBfrom=loxl.workbooks.open(lcFrom2)
lowbto.sheets.add.name="Tab3"
lowbto.Sheets('Tab3').Move(,lowbto.Sheets(3))
lowbfrom.sheets(1).activate
loxl.cells.select
loxl.selection.copy
lowbto.sheets('Tab3').activate
loxl.Selection.pastespecial
loxl.range("A1:A1").select
lowbfrom.application.cutcopymode = .f.
loWBfrom.close(0)
lowbto.sheets('Tab1').activate
loxl.range("A1:A1").select
lowbto.sheets(1).activate
lowbto.save
loXL.visible=.t.
ELSE
=MESSAGEBOX('Error Opening Excel. Cannot create XLS report',16,'Error Message')
ENDIF