ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating a Named Range using VB (https://www.excelbanter.com/excel-programming/284554-creating-named-range-using-vbulletin.html)

Adriaan van der Linde

Creating a Named Range using VB
 
Hi Guys

I want to create a Named Range making use of a dynamic
amount of rows in a sheet.

The examples I have will only accept a predefined range.

' I can select the range I want to name
ActiveSheet.Range("AD2", ActiveSheet.Range("AD2").End
(xlDown)).Select

' I now want to take that selection and name the
range
ActiveWorkbook.Names.Add Name:="DropListLoc",
RefersToR1C1:="=NewProject!R9C5"

This will only name the "C5" cell

Thanks a stack

Adriaan




WillR[_2_]

Creating a Named Range using VB
 

try this code


Code
-------------------
Sub NameARange()
Range("AD2:AD" & Range("AD2").End(xlDown).Row).Name = "MyName"
End Su
-------------------


Hope this helps,

Wil

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com

~~Now Available: Financial Statements.xls, a step by step guide to creating financial statements

MattShoreson[_4_]

Creating a Named Range using VB
 

or...

instead of RefersToR1C1

use

RefersTo:=Range("A1:B2"

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com

~~Now Available: Financial Statements.xls, a step by step guide to creating financial statements

Bob Phillips[_6_]

Creating a Named Range using VB
 
Adrian ,

Try

Dim sRange As String
With ActiveSheet
sRange = .Range("AD2", .Range("AD2").End(xlDown)).Address
ActiveWorkbook.Names.Add Name:="DropListLoc", _
RefersTo:="=" & sRange
End With


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Adriaan van der Linde" wrote in
message ...
Hi Guys

I want to create a Named Range making use of a dynamic
amount of rows in a sheet.

The examples I have will only accept a predefined range.

' I can select the range I want to name
ActiveSheet.Range("AD2", ActiveSheet.Range("AD2").End
(xlDown)).Select

' I now want to take that selection and name the
range
ActiveWorkbook.Names.Add Name:="DropListLoc",
RefersToR1C1:="=NewProject!R9C5"

This will only name the "C5" cell

Thanks a stack

Adriaan






Shailesh Shah[_2_]

Creating a Named Range using VB
 
Hi Adriaan,

Try this code. Assume [AD2] is not blank,

Sub CreateName()

Dim cell As Range, rng As Range, sName As String

sName = "DropListLoc"

Set cell = [AD2]

Set rng = ActiveSheet.Range(cell, cell.End(xlDown))

If Application.CountA(rng) 1 Then

rng.Name = sName

Else

cell.Name = sName

End If

End Sub


Regards,
Shah Shailesh
http://members.lycos.co.uk/shahweb/
(Excel Add-ins)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


All times are GMT +1. The time now is 08:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com