Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.Name property in event Worksheet_SelectionChange

Status
Not open for further replies.

Malachy

Programmer
Oct 4, 2002
3
GB
Hello,

I'm trying to pass the Range Name of the active cell every time the Worksheet_SelectionChange event is called.

This code doesn't work...

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

 Dim rangeString As String
    
    rangeString = ActiveCell.name   
    Call Worksheet_highlight(rangeString)

End Sub

Can anyone advise why ActiveCell.Name returns the string &quot;='Sheet1!$A$1&quot; if cell A1 has a name eg &quot;firstCell&quot;, but returns an error if cell A1 does not have a name; in debug mode the 'tool tip' shows &quot;ActiveCell.name = <Application-defined or Object-defined error>.

In this example, I want to pass the string &quot;firstCell&quot; - Any ideas?

Thank you,

M.
 
Malacy,

The Name property of the Range object returns a Name object. The Name object itself has a Name property. So, to get to the string value that constitutes a &quot;named range&quot; you need syntax such as

Code:
rangestring = Activecell.Name.Name

you need to also add
Code:
On Error Resume Next
before this line to bypass the error condition (the 1004 error) that will occur if a particular cell does not have a named range.

HTH
Mike
 
Hi Mike,

Thank you that was very helpful - I thought it must be something quite straight forward. You live, you learn! Thanks for the tip on error trapping - it's high time my code included some of that.

M.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top