Range Object Question

A

abillmeier

This may be more of an Excel Object/VBA question, but here goes...

I am working on a VSTO excel project, being developed in version 2003. I am
running through a series of excel Range Objects that are named ranges in a
workbook. Is there a way to identify the Name of the range in code?

I have a data mappings page that ties the ranges to a custom Object's
properties and I need to grab the Name associated with the active range/cell
so I can shove it in the object.

Any tips would be great.

thanks
 
T

Tom Ogilvy

selection.Name.Name (range object.Name returns a Name object )

if it doesn't have a name, you get an error. the selection must represent
the entirety of the cells in the range -

for example if the Selection is A1 and the range A1:B2 is named Horse, then
you would get an error. If the selection in A1:B2, then it would return
Horse.

This is how I would do it in VBA:

Sub TestName()
Dim nm As Name
On Error Resume Next
Set nm = Selection.Name
On Error GoTo 0
If Not nm Is Nothing Then
MsgBox nm.Name
Else
MsgBox "no name"
End If
End Sub
 
A

abillmeier

Thanks for the assist, works great.


Tom Ogilvy said:
selection.Name.Name (range object.Name returns a Name object )

if it doesn't have a name, you get an error. the selection must
represent
the entirety of the cells in the range -

for example if the Selection is A1 and the range A1:B2 is named Horse,
then
you would get an error. If the selection in A1:B2, then it would return
Horse.

This is how I would do it in VBA:

Sub TestName()
Dim nm As Name
On Error Resume Next
Set nm = Selection.Name
On Error GoTo 0
If Not nm Is Nothing Then
MsgBox nm.Name
Else
MsgBox "no name"
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top