Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Macro to Define Name on a changing selection of cells

I am trying to write a macro that uses Define Name to name a range of cells.
The range is always changing as rows and columns get added/deleted, so I
tried using 'Selection.End' functionality. However, when it gets to RefersTo
in Define Name, it hard codes the range instead of using the currently
selected range. Any thoughts? Thanks!

Sub Naming()
'
' Naming Macro
'

'
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add name:="Timeline", RefersToR1C1:= _
"=Timeline!R4C2:R49C66"
ActiveWorkbook.Names("Timeline").Comment = ""
Range("B2").Select
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Macro to Define Name on a changing selection of cells

If you are selecting a range, either manually or with VBA, just add a line
like:
Selection.Name="TheName"
"Kori" wrote in message
...
I am trying to write a macro that uses Define Name to name a range of
cells.
The range is always changing as rows and columns get added/deleted, so I
tried using 'Selection.End' functionality. However, when it gets to
RefersTo
in Define Name, it hard codes the range instead of using the currently
selected range. Any thoughts? Thanks!

Sub Naming()
'
' Naming Macro
'

'
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add name:="Timeline", RefersToR1C1:= _
"=Timeline!R4C2:R49C66"
ActiveWorkbook.Names("Timeline").Comment = ""
Range("B2").Select
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Macro to Define Name on a changing selection of cells

Per your criteria:

Application.Names.Add Name:="Timeline", _
RefersTo:=Range([b4], [b4].End(xlDown).End(xlToRight))

or

Application.Names.Add Name:="RangeName", _
RefersTo:=Range(Range("b4"), Range("b4").End(xlDown).End(xlToRight))


--
MacGuy


"Kori" wrote:

I am trying to write a macro that uses Define Name to name a range of cells.
The range is always changing as rows and columns get added/deleted, so I
tried using 'Selection.End' functionality. However, when it gets to RefersTo
in Define Name, it hard codes the range instead of using the currently
selected range. Any thoughts? Thanks!

Sub Naming()
'
' Naming Macro
'

'
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add name:="Timeline", RefersToR1C1:= _
"=Timeline!R4C2:R49C66"
ActiveWorkbook.Names("Timeline").Comment = ""
Range("B2").Select
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Macro to Define Name on a changing selection of cells

I forgot to add the .end(xl___) functionality works as long as there are no
blank cells in the first column (xlDown) and the last row (xlToRight)

Consider:

Application.Names.Add Name:="Timeline", _
RefersTo:=Range(Range("b4"), Range("b4").CurrentRegion)

or

Application.Names.Add Name:="Timeline", _
RefersTo:=Range([b4].Offset(ActiveSheet.UsedRange.Rows.Count - 1,
0), _
[b4].Offset(0, ActiveSheet.UsedRange.Columns.Count - 1))


--
MacGuy


"MacGuy" wrote:

Per your criteria:

Application.Names.Add Name:="Timeline", _
RefersTo:=Range([b4], [b4].End(xlDown).End(xlToRight))

or

Application.Names.Add Name:="RangeName", _
RefersTo:=Range(Range("b4"), Range("b4").End(xlDown).End(xlToRight))


--
MacGuy


"Kori" wrote:

I am trying to write a macro that uses Define Name to name a range of cells.
The range is always changing as rows and columns get added/deleted, so I
tried using 'Selection.End' functionality. However, when it gets to RefersTo
in Define Name, it hard codes the range instead of using the currently
selected range. Any thoughts? Thanks!

Sub Naming()
'
' Naming Macro
'

'
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add name:="Timeline", RefersToR1C1:= _
"=Timeline!R4C2:R49C66"
ActiveWorkbook.Names("Timeline").Comment = ""
Range("B2").Select
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Macro to Define Name on a changing selection of cells

Thank you!!!

"MacGuy" wrote:

I forgot to add the .end(xl___) functionality works as long as there are no
blank cells in the first column (xlDown) and the last row (xlToRight)

Consider:

Application.Names.Add Name:="Timeline", _
RefersTo:=Range(Range("b4"), Range("b4").CurrentRegion)

or

Application.Names.Add Name:="Timeline", _
RefersTo:=Range([b4].Offset(ActiveSheet.UsedRange.Rows.Count - 1,
0), _
[b4].Offset(0, ActiveSheet.UsedRange.Columns.Count - 1))


--
MacGuy


"MacGuy" wrote:

Per your criteria:

Application.Names.Add Name:="Timeline", _
RefersTo:=Range([b4], [b4].End(xlDown).End(xlToRight))

or

Application.Names.Add Name:="RangeName", _
RefersTo:=Range(Range("b4"), Range("b4").End(xlDown).End(xlToRight))


--
MacGuy


"Kori" wrote:

I am trying to write a macro that uses Define Name to name a range of cells.
The range is always changing as rows and columns get added/deleted, so I
tried using 'Selection.End' functionality. However, when it gets to RefersTo
in Define Name, it hard codes the range instead of using the currently
selected range. Any thoughts? Thanks!

Sub Naming()
'
' Naming Macro
'

'
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add name:="Timeline", RefersToR1C1:= _
"=Timeline!R4C2:R49C66"
ActiveWorkbook.Names("Timeline").Comment = ""
Range("B2").Select
End Sub



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
Trying to define a changing range for chart Rcarper Charts and Charting in Excel 2 January 14th 09 03:50 PM
Problem with Cells Selection through macro. Rahul Gupta Excel Programming 2 November 4th 06 02:55 PM
Macro to select cells and then new selection from selected cells Rewop Eilsel Excel Programming 0 June 6th 06 04:25 PM
How to automatically clear cells upon changing/making selection? Ingeniero1[_8_] Excel Programming 3 April 17th 06 09:44 PM
cells not changing selection after sheets array Simon Lloyd[_477_] Excel Programming 4 June 9th 04 03:23 PM


All times are GMT +1. The time now is 08:38 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"