473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Excel workbook copy to existing workbook

2
Looking at code examples, it would seem this is simple, but I can't figure it out. From an Access 2000 form command button, I want to copy all worksheets in an existing workbook to an existing workbook and save the results in a new workbook. I keep getting the message "subscript out of range." Any help is greatly appreciated.

Private Sub Copier_Click()
' Copy all sheets in FileA before sheets in FileMaster and save as NewFile

Dim mysheet As Variant
Dim sheet As Variant
Dim FileName As String
Dim X As Integer

Set xlApp = CreateObject("Excel.Application")
Set mysheet = xlApp.Workbooks.Open("C:\FileA.xls").Sheets(1)
Set sheet = xlApp.ActiveWorkbook.Sheets(1)
For X = 1 To xlApp.ActiveWorkbook.Sheets.Count
'Loop through each of the sheets in the workbook
xlApp.ActiveWorkbook.Sheets(X).Copy _
Before:=xlApp.Workbooks("C:\FileMaster.xls").Sheet s(1)
Next

FileName = "C:\NewFile.xls"
mysheet.Application.ActiveWorkbook.SaveAs FileName

End Sub
Mar 4 '08 #1
3 3796
FishVal
2,653 Expert 2GB
Hi, there.

I guess your have two different instances of Excel.Application so you cannot refer to Workbooks collection via the same Excel.Application object.

Regards,
Fish
Mar 4 '08 #2
samj
2
Hi, there.

I guess your have two different instances of Excel.Application so you cannot refer to Workbooks collection via the same Excel.Application object.

Regards,
Fish
I've checked task manager, and only one instance is running. I'm new to Excel automation, so figure I've got something wrong in syntax. Code works fine to create new workbook from just FileA, but problem occurs when trying to combine with another existing workbook (FileMaster). Any thoughts?
Mar 5 '08 #3
FishVal
2,653 Expert 2GB
In order to copy to Workbooks("C:\FileMaster.xls") you need to open it just as "C:FileA.xls".
Additionally I'd like to say that your code is somewhat unstraight and hazy. Not clear why do you reference worksheets by variables sheet and mysheet, and, at the same time you don't have references to source and destination workbooks, which would be more useful of course. And so on, and so on.
Mar 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris | last post by:
Hi, I am using: DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, lcQueryName, strFilePathNamE, True ....to export my query data to an existing excel workbook. Then what i need...
4
by: sunilkeswani | last post by:
I need help with exporting data from 2 access tables, into 2 existing spreadsheets in a single Excel file. Currently, I am using this code: DoCmd.TransferSpreadsheet acExport, 8, "Table1",...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
8
by: Sam | last post by:
Hi All, I posted this message few days ago. No one has answered so far. I just would like to know if it is possible to do or not. Even if you can not tell me how to do this, maybe you know which...
1
by: RickH | last post by:
..Cells(1,y).GetType ownly shows instead of .Value, .Copy, etc. The code below is derived from samples, it should work, but I've messed up somewhere... Imports System.Windows.Forms Imports...
1
by: gssstuff | last post by:
Hi I have a need to send data from Access to an existing Excel template that I am using for reporting purposes. The nature of the data in the Access data table does not lend itself to a simple...
5
by: hmiller | last post by:
Hey there folks: I have been trying to get this work for about a week now. I'm new to VBA... I am trying to transfer a populated table in Access to an existing, but blank, Excel worksheet. I...
18
by: John Bailo | last post by:
I want to write an Excel file (.xls format) from some database data. I don't want to use Excel.exe because of all the automation and security issues. Does Microsoft document the .xls file...
7
by: NeverLift | last post by:
This is probably answered elsewhere, but I've searched the Web and VBA for Excel manual, find no answers. I have a VBA-coded macro in an Excel workbook that is to open another existing workbook --...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.