View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Otto Moehrbach[_2_] Otto Moehrbach[_2_] is offline
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