#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default Named Range

I'm creating a template in which a user can copy & paste their records and
format it a certain way w/a macro button. One portion of the macro should
create a named range for all records that appear in column J, starting at J4.
However, as you can see from my code the marco is always going to name
whatever is in between J4:J748 (this is how many records are in the document
I am working with).

I can't figure out how to fix it - any suggestions appreciated.

Range("J4").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="Records2", RefersToR1C1:= _
"=Sheet1!R4C10:R748C10"
Range("C1").Select
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Named Range

Hi,

Try this

Dim LastRow As Long
Set Sht = Sheets("Sheet1") ' Change to suit
LastRow = Cells(Cells.Rows.Count, "J").End(xlUp).Row
ActiveWorkbook.Names.Add Name:="Records2", _
RefersTo:=Sht.Range("J4:J" & LastRow)
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Munchkin" wrote:

I'm creating a template in which a user can copy & paste their records and
format it a certain way w/a macro button. One portion of the macro should
create a named range for all records that appear in column J, starting at J4.
However, as you can see from my code the marco is always going to name
whatever is in between J4:J748 (this is how many records are in the document
I am working with).

I can't figure out how to fix it - any suggestions appreciated.

Range("J4").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="Records2", RefersToR1C1:= _
"=Sheet1!R4C10:R748C10"
Range("C1").Select

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Named Range

oops,

a bug in that one, try this instead

Dim LastRow As Long
Set Sht = Sheets("Sheet1") ' Change to suit
LastRow = Sht.Cells(Cells.Rows.Count, "J").End(xlUp).Row
ActiveWorkbook.Names.Add Name:="Records2", _
RefersTo:=Sht.Range("J4:J" & LastRow)
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Mike H" wrote:

Hi,

Try this

Dim LastRow As Long
Set Sht = Sheets("Sheet1") ' Change to suit
LastRow = Cells(Cells.Rows.Count, "J").End(xlUp).Row
ActiveWorkbook.Names.Add Name:="Records2", _
RefersTo:=Sht.Range("J4:J" & LastRow)
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Munchkin" wrote:

I'm creating a template in which a user can copy & paste their records and
format it a certain way w/a macro button. One portion of the macro should
create a named range for all records that appear in column J, starting at J4.
However, as you can see from my code the marco is always going to name
whatever is in between J4:J748 (this is how many records are in the document
I am working with).

I can't figure out how to fix it - any suggestions appreciated.

Range("J4").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="Records2", RefersToR1C1:= _
"=Sheet1!R4C10:R748C10"
Range("C1").Select

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Named Range

Something like this worked for me. It will allow the named range to vary
based on the number of records in column A. Of course it can be altered to
suit individual needs.

Sub dl() '<<<Can change to CommandButton1_Click()
Dim lr As Long, sh As Worksheet, rng As Range
Set sh = ActiveSheet
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh.Range("A2:A" & lr)
ActiveWorkbook.Names.Add "TestRange", RefersTo:=rng.Address
End Sub


"Munchkin" wrote in message
...
I'm creating a template in which a user can copy & paste their records and
format it a certain way w/a macro button. One portion of the macro should
create a named range for all records that appear in column J, starting at
J4.
However, as you can see from my code the marco is always going to name
whatever is in between J4:J748 (this is how many records are in the
document
I am working with).

I can't figure out how to fix it - any suggestions appreciated.

Range("J4").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="Records2", RefersToR1C1:= _
"=Sheet1!R4C10:R748C10"
Range("C1").Select



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 76
Default Named Range

Munchkin,

You have already Selected the range you want to name, so the following
should work for you.

ActiveWorkbook.Names.Add Name:="Records2", RefersTo:=Selection.Address

I did not test this. -- Brad E.


"Munchkin" wrote:

I'm creating a template in which a user can copy & paste their records and
format it a certain way w/a macro button. One portion of the macro should
create a named range for all records that appear in column J, starting at J4.
However, as you can see from my code the marco is always going to name
whatever is in between J4:J748 (this is how many records are in the document
I am working with).

I can't figure out how to fix it - any suggestions appreciated.

Range("J4").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="Records2", RefersToR1C1:= _
"=Sheet1!R4C10:R748C10"
Range("C1").Select

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
Delete Named rnage and Add Named Range ryguy7272 Excel Programming 5 February 5th 10 10:15 AM
Selecting a named range, the name of the named range is in a cell Luc[_8_] Excel Programming 6 January 13th 10 07:35 PM
Array as a "named range" - formula ok in cells, but error as "named range" tskogstrom Excel Discussion (Misc queries) 11 December 28th 06 04:44 PM
inserting a named range into new cells based on a named cell Peter S. Excel Discussion (Misc queries) 1 June 4th 06 03:53 AM
If any cell in named range = 8 then shade named range JJ[_8_] Excel Programming 3 August 26th 05 11:09 PM


All times are GMT +1. The time now is 12:18 AM.

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

About Us

"It's about Microsoft Excel"