i am writting a code to copy a sheet in excel from one workbook to another and I’m having the following issue :
Set xlsAppSource = CreateObject("Excel.Application")
Set xlsWBSource = xlsAppSource.Workbooks.Open(strWBSource)
Set xlsAppCible = CreateObject("Excel.Application")
Set xlsWBCible = xlsAppCible.Workbooks.Open(strWBCible)
xlsWBSource.Sheets(1).Copy before:= xlsWBSource.Sheets(1)
And I get ERROR 1004.
I know the error comes from :
xlsWBSource.Sheets(1) and xlsWBSource.Sheets(1)
because when change the last statement to :
xlsWBSource.activate
ActiveWorkbook.Sheets(strSheetSource).Copy before:= [any working sheet]
it works.
But As I cannot activate the 2 workbooks it’s not a way to solve my problem.
Any ideas why it doesnt work ?
thanks
If you are tring to copy from from
xlsWBSourcetoxlsWBCiblethen you should only use one instance of Excel , you are currently opening two separate versions with your twoCreateObjects– the two separate instances can’t “talk”, hence the errorWhereas this will work: