PDA

View Full Version : Copy Excel sheet into an existing Excel file



StephenL
08-10-2006, 02:18 PM
Hello all,

Is it possible to copy an Excel worksheet into an existing Excel file using Access VBA coding?


Dim xlApp As Object
Dim ExlFile As Object
Dim ExlFile2 As Object
Dim FN1 as String
Dim FN2 as String

FN1 = "c:\YTD Reports summary.xls"
FN2 = "c:\ YTD Reports by BU.xls"

Set xlApp = CreateObject("Excel.Application")
Set ExlFile = xlApp.workbooks.Open(FN1).Sheets(1)
Set ExlFile2 = xlApp.workbooks.Open(FN2).Sheets(1)

ExlFile.Copy After:=ExlFile2 ? This is where I am stuck.


Any help would be greatly appreciated.

Steve

geekgirlau
08-10-2006, 11:48 PM
If you declare ExlFile and ExlFile2 as "Worksheet" rather than "Object" this should work (you'll need a reference to the Excel object library in order to do this).

StephenL
08-11-2006, 07:09 AM
Thanks - That worked.

Norie
08-11-2006, 09:11 AM
Steve

The code you posted should work, how didn't it?

By the way declaring as Worksheet and referencing the Excel Object library is good programming practice.

But it's not really needed, and could in fact cause problems if you are distributing this code to or it will be used by people/machines with different versions of Excel.

StephenL
08-11-2006, 11:24 AM
Norie - I was getting subscript errors. :banghead:

Norie
08-11-2006, 11:45 AM
Steve

Can't quite see how that code would cause that.:dunno