[AccessD] Export to Excel via VBA, Naming Cells

Hale, Jim Jim.Hale at FleetPride.com
Fri Mar 17 14:50:17 CST 2006


This works:
ActiveWorkbook.Names.Add Name:="test1", RefersTo:="=Sheet1!$a$1:$b$3"
 Are you missing a : after the f?
Jim Hale

-----Original Message-----
From: Lawrence Mrazek [mailto:lmrazek at lcm-res.com]
Sent: Friday, March 17, 2006 12:32 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Export to Excel via VBA, Naming Cells


Thanks Gustav & Jim:

I guess I'm just having trouble with the syntax of creating a named range
from VBA. For example, using my code (below), how would I assign a name?
Right now, the names are getting added to the sheet, but the references to
the cells are messed up. Something is not working when I set the
CellLocation variable ...

While Not rsSales.EOF

            xlWs.Cells(rownum, 1) = rsSales.Fields("SalesRep")
            xlWs.Cells(rownum, 6) = rsSales.Fields("month")
		xlWs.Cells(rownum, 7) = rsSales.Fields("KGsLastYr")
		RangeName = rsSales.Fields("SalesRep") &
rsSales.Fields("itemtype") & rsSales.Fields("month")
            CellLocation = "'" & WrkSheetName & "'" & "!$" & "f" & "$" &
rownum 
            xlWb.Names.Add Name:=RangeName, RefersTo:=CellLocation,
Visible:=True      	    '
                   
rownum = rownum + 1
rsSales.MoveNext

Wend

Thanks!

Larry Mrazek
LCM Research, Inc.
www.lcm-res.com
lmrazek at lcm-res.com
ph. 314-432-5886
fx. 314-432-3304

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Friday, March 17, 2006 11:05 AM
To: accessd at databaseadvisors.com
Subject: Re: [AccessD] Export to Excel via VBA, Naming Cells

Hi Larry

Yes, a 'dynamic named range' is every bit as much a 'named range' as a
static one; the only difference is that instead of the Refers To formula
looking like:

    =Sheet1!$A$1:$D$100

it looks more like:

    =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),4)

or some other formula whose result is a range.
However, you cannot link to a dynamic range as a table in Access as Excel
needs to be open to get the coordinates of the range.
But via code you can do that calculation in many other ways and then create
a static named range.

/gustav


>>> lmrazek at lcm-res.com 17-03-2006 17:37:27 >>>

***********************************************************************
The information transmitted is intended solely for the individual or
entity to which it is addressed and may contain confidential and/or
privileged material. Any review, retransmission, dissemination or
other use of or taking action in reliance upon this information by
persons or entities other than the intended recipient is prohibited.
If you have received this email in error please contact the sender and
delete the material from any computer. As a recipient of this email,
you are responsible for screening its contents and the contents of any
attachments for the presence of viruses. No liability is accepted for
any damages caused by any virus transmitted by this email.


More information about the AccessD mailing list