Marshall Barton wrote ...
Would you expand on this Jamie?
I was surprised to see that I can indeed get SELECT INTO to
create a new xls file and a new sheet with the specified
name. However, I can't get it to create a new sheet in an
existing file (can't find object error, Excel 2002).
Here's a demo. First create a totally new workbook: we can use the
CREATE TABLE syntax to do this:
CREATE TABLE
[Excel 8.0;HDR=YES;Database=C:\TotallyNew.xls;].MySheet1
(MyCol1 FLOAT NULL)
;
OK, so we have an existing workbook. Now use the SELECT..INTO syntax
to create a new worksheet in the existing workbook:
SELECT
null AS MyCol2
INTO
[Excel 8.0;HDR=YES;Database=C:\TotallyNew.xls;].MySheet2
;
Since there's no way to specify the location of the range, I
don't see how a query could possibly create a new named
range.
In short, the SELECT..INTO needs to be able to create a new worksheet.
The above syntax requires the table specified to be a workbook-level
Excel defined Name (aka a 'named Range'). Specifying a worksheet e.g.
[MySheet2$] would cause the SELECT..INTO to fail (the cause of your
'can't find object' error, perhaps?), as would specifying a defined
Name that already exists.
Usually, the provider will create a new sheet with the same name as
the specified defined Name. If the sheet, but not he defined Name,
already exists and is clear (you can easily create these circumstances
by issuing a DROP TABLE on the defined Name i.e. the Name's definition
and data is removed but not the sheet) then the SELECT..INTO creates
the defined Name on the existing sheet of the same name. However, if
the existing sheet of the same name is 'dirty' i.e. (create these
circumstances by opening the workbook in the Excel UI and delete the
defined Name definition but not the data) then a new sheet is created
with by appending a number to the sheet name e.g. if MySheet1 already
exists, the defined Name named MySheet2 is created on a new sheet
named MySheet21; if MySheet21 exists the new sheet will be MySheet22.
If you require any more details, post back.
PS Sorry, I can't resist but do you know there is an industrial park
in my local town named after you:
http://www.google.com/search?q="marsh+barton+is"