Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Does the selected cell have a range name?

I am trying to determine at runtime whether the selected cell has a range
name, and what that name is. This is to error trap the user accidentally
selecting a row that is a category header (column A of these rows are named)
as opposed to a row containing content (column A not named). If I use

Range("A" & intRow).Name

it shows "=Sheet!$A$13" instead of the name I'm looking for

Any help appreciated.

Bill Le May


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Does the selected cell have a range name?

In your Worksheet.selectionchange event, you could put something like this

If not intersect(target,range("RangeName1")) is nothing then
Msgbox("You've selected a cell within RangeName1")
end if


"Bill" wrote:

I am trying to determine at runtime whether the selected cell has a range
name, and what that name is. This is to error trap the user accidentally
selecting a row that is a category header (column A of these rows are named)
as opposed to a row containing content (column A not named). If I use

Range("A" & intRow).Name

it shows "=Sheet!$A$13" instead of the name I'm looking for

Any help appreciated.

Bill Le May



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default Does the selected cell have a range name?

You need to look for the Name property of the name object (yes its
confusing!)

Range("A" & intRow).Name.Name

Charles
______________________
Decision Models
FastExcel 2.3 now available
Name Manager 4.0 now available
www.DecisionModels.com

"Bill" wrote in message
...
I am trying to determine at runtime whether the selected cell has a range
name, and what that name is. This is to error trap the user accidentally
selecting a row that is a category header (column A of these rows are
named) as opposed to a row containing content (column A not named). If I
use

Range("A" & intRow).Name

it shows "=Sheet!$A$13" instead of the name I'm looking for

Any help appreciated.

Bill Le May



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Does the selected cell have a range name?

Range("A" & intRow).Name

refers to the Name object associated with the cell. The default property
for the .Name object is the .RefersTo property, so that's what you're
getting.

The Name's Name property is returned by

Range("A" & intRow).Name.Name

If you want to test whether the cell has a name, and return the name if
it does, here's one way:

Public Function CellName(ByRef rCell As Range) As String
Dim nm As Name
Dim rTest As Range
Dim sResult As String

On Error GoTo ErrorHandler
For Each nm In ActiveWorkbook.Names
Set rTest = nm.RefersToRange
If Not rTest Is Nothing Then
With rTest
If .Parent.Name = rCell.Parent.Name Then
If Not Intersect(.Cells, rCell) Is Nothing Then
sResult = nm.Name
Exit For
End If
End If
End With
End If
Next nm
If sResult = vbNullString Then
For Each nm In rCell.Parent.Names
Set rTest = nm.RefersToRange
If Not rTest Is Nothing Then
If Not Intersect(rTest.Cells, rCell) Is Nothing Then
sResult = nm.Name
Exit For
End If
End If
Next nm
End If
If sResult < vbNullString Then CellName = sResult
ResumeHe
Exit Function
ErrorHandler:
If Err.Number = 1004 Then 'name doesn't refer to a range
Set rTest = Nothing
Resume Next
Else
CellName = CVErr(xlErrValue)
Resume ResumeHere
End If
End Function

The function returns a null string if the cell is not part of a named
range.

In article ,
"Bill" wrote:

I am trying to determine at runtime whether the selected cell has a range
name, and what that name is. This is to error trap the user accidentally
selecting a row that is a category header (column A of these rows are named)
as opposed to a row containing content (column A not named). If I use

Range("A" & intRow).Name

it shows "=Sheet!$A$13" instead of the name I'm looking for

Any help appreciated.

Bill Le May

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Does the selected cell have a range name?

Oops, took this from one of my previous applications and forgot to make
one change. Change

For Each nm In ActiveWorkbook.Names

to

For Each nm In rCell.Parent.Parent.Names

In article ,
JE McGimpsey wrote:

Range("A" & intRow).Name

refers to the Name object associated with the cell. The default property
for the .Name object is the .RefersTo property, so that's what you're
getting.

The Name's Name property is returned by

Range("A" & intRow).Name.Name

If you want to test whether the cell has a name, and return the name if
it does, here's one way:

Public Function CellName(ByRef rCell As Range) As String
Dim nm As Name
Dim rTest As Range
Dim sResult As String

On Error GoTo ErrorHandler
For Each nm In ActiveWorkbook.Names
Set rTest = nm.RefersToRange
If Not rTest Is Nothing Then
With rTest
If .Parent.Name = rCell.Parent.Name Then
If Not Intersect(.Cells, rCell) Is Nothing Then
sResult = nm.Name
Exit For
End If
End If
End With
End If
Next nm
If sResult = vbNullString Then
For Each nm In rCell.Parent.Names
Set rTest = nm.RefersToRange
If Not rTest Is Nothing Then
If Not Intersect(rTest.Cells, rCell) Is Nothing Then
sResult = nm.Name
Exit For
End If
End If
Next nm
End If
If sResult < vbNullString Then CellName = sResult
ResumeHe
Exit Function
ErrorHandler:
If Err.Number = 1004 Then 'name doesn't refer to a range
Set rTest = Nothing
Resume Next
Else
CellName = CVErr(xlErrValue)
Resume ResumeHere
End If
End Function

The function returns a null string if the cell is not part of a named
range.

In article ,
"Bill" wrote:

I am trying to determine at runtime whether the selected cell has a range
name, and what that name is. This is to error trap the user accidentally
selecting a row that is a category header (column A of these rows are
named)
as opposed to a row containing content (column A not named). If I use

Range("A" & intRow).Name

it shows "=Sheet!$A$13" instead of the name I'm looking for

Any help appreciated.

Bill Le May



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Does the selected cell have a range name?

Thanks to all for the help!

Bill Le May

"Charles Williams" wrote in message
...
You need to look for the Name property of the name object (yes its
confusing!)

Range("A" & intRow).Name.Name

Charles
______________________
Decision Models
FastExcel 2.3 now available
Name Manager 4.0 now available
www.DecisionModels.com



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
Determining Range of selected cell [email protected] Excel Programming 2 December 5th 06 06:49 AM
Referencing the First Cell in Any Selected Range maximouse Excel Programming 4 November 1st 05 03:57 AM
Macro run if and cell in range is selected in VBA Celtic_Avenger[_38_] Excel Programming 1 September 18th 04 04:51 PM
Macro run if and cell in range is selected in VBA Celtic_Avenger[_37_] Excel Programming 1 September 18th 04 03:28 PM
Activate last cell in selected range - an example DataFreakFromUtah Excel Programming 11 September 5th 04 02:02 AM


All times are GMT +1. The time now is 03:22 PM.

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"