Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Defining a named range for a dynamic result set

In a workbook template that will be run for each months production numbers,
several hidden worksheets are updated with data from MS query from an Access
db. The first column of each contains a value used to discriminate between
new or renewal business, while the second column is used by worksheets the
user can see to automatically display data using VLOOKUP functions. I have
manually defined the range of data for new and renewal, but would like to
create VBA code that will do it automatically when the template is opened
each month. I am having trouble redefining the range once I know its
dimensions.

When I record the range define steps in a macro, the following code is
generated:

Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add Name:="rngPrgmYTD", RefersToR1C1:= _
"='LU-PRGMYTD'!R1C1:R14C8"

How would I replace the 14 and 8 in the R14C8 reference with variables?

Also, assuming the name already exists, what would the statement syntax be
to resize the already-defined named range?

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Defining a named range for a dynamic result set

I'd use something like:

dim myRng as range
Dim LastRow as long
Dim LastCol as long
with activesheet
lastrow = .cells(.rows.count,"A").end(xlup).row
lastCol = .cells(1,.columns.count).end(xltoleft).column

set myrng = .range("A1",.cells(lastrow,lastcol))
end with

myRng.name = "rngPrgmYTD"

This actually uses the last used column in Row 1 and the last used row in column
A.

Keith B. wrote:

In a workbook template that will be run for each months production numbers,
several hidden worksheets are updated with data from MS query from an Access
db. The first column of each contains a value used to discriminate between
new or renewal business, while the second column is used by worksheets the
user can see to automatically display data using VLOOKUP functions. I have
manually defined the range of data for new and renewal, but would like to
create VBA code that will do it automatically when the template is opened
each month. I am having trouble redefining the range once I know its
dimensions.

When I record the range define steps in a macro, the following code is
generated:

Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add Name:="rngPrgmYTD", RefersToR1C1:= _
"='LU-PRGMYTD'!R1C1:R14C8"

How would I replace the 14 and 8 in the R14C8 reference with variables?

Also, assuming the name already exists, what would the statement syntax be
to resize the already-defined named range?

Thanks


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Defining a named range for a dynamic result set

Hi Keith,

You might want to define the range as a dynamic range so when your code
changes its size, Excel adjusts for that automatically. You can get more info
he

http://www.contextures.com/xlNames01.html#Dynamic

HTH
Regards,
Garry
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking up a returned value defining a named range DRC Excel Discussion (Misc queries) 2 March 23rd 10 02:49 PM
Defining a custom-named Range across several worksheets Sven Herremans Excel Worksheet Functions 3 October 23rd 07 12:23 PM
help defining dynamic range joecrabtree Charts and Charting in Excel 0 December 6th 06 03:33 PM
Defining a Dynamic Range using a variable Alseikhan[_4_] Excel Programming 3 March 27th 06 08:56 AM
Defining Dynamic Range Jamie[_11_] Excel Programming 4 October 27th 05 06:36 PM


All times are GMT +1. The time now is 07:51 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"