Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Testing for named cells (i.e., ranges)

The following code is written to test for the existence of a cell named
"anchor" on the Active Sheet but it does not work. If the named cell exists,
that named range is supposed to become the active cell.

Something is wrong with the second statement. Even if a cell named "anchor"
exists on the active sheet, the resulting CellName is Nothing. Why?

Dim CellName as Name

On Error Resume Next
Set CellName = ActiveSheet.Names("anchor").Name
On Error GoTo 0
If Not CellName Is Nothing Then
Application.Goto Reference:=CellName
Else
Application.Goto Reference:=Range("A1")
End If

Thank you.

John Wirt


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Testing for named cells (i.e., ranges)

ActiveSheet.Names will only return names which are specific to that
Worksheet. ie. Sheet1!Anchor

Try this:

Sub test()
Dim strName As String, nam As Name

strName = "anchor"

On Error Resume Next
Set nam = ActiveSheet.Names(strName)
If nam Is Nothing Then Set nam = Names(strName)
If Not nam.RefersToRange.Worksheet Is ActiveSheet Then Set nam = Nothing
On Error GoTo 0
If Not nam Is Nothing Then
nam.RefersToRange.Select
Else
Range("A1").Select
End If
End Sub



--
Rob van Gelder - http://www.vangelder.co.nz/excel


"John Wirt" wrote in message
...
The following code is written to test for the existence of a cell named
"anchor" on the Active Sheet but it does not work. If the named cell
exists,
that named range is supposed to become the active cell.

Something is wrong with the second statement. Even if a cell named
"anchor"
exists on the active sheet, the resulting CellName is Nothing. Why?

Dim CellName as Name

On Error Resume Next
Set CellName = ActiveSheet.Names("anchor").Name
On Error GoTo 0
If Not CellName Is Nothing Then
Application.Goto Reference:=CellName
Else
Application.Goto Reference:=Range("A1")
End If

Thank you.

John Wirt




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Testing for named cells (i.e., ranges)

John,
try this,

Sub DoIt()
On Error GoTo RngError
Range("anchor").Select
Exit Sub
RngError:
Application.Goto Reference:=Range("A1")
Exit Sub
End Sub

Regards
Neil

"John Wirt" wrote in message
...
The following code is written to test for the existence of a cell named
"anchor" on the Active Sheet but it does not work. If the named cell
exists,
that named range is supposed to become the active cell.

Something is wrong with the second statement. Even if a cell named
"anchor"
exists on the active sheet, the resulting CellName is Nothing. Why?

Dim CellName as Name

On Error Resume Next
Set CellName = ActiveSheet.Names("anchor").Name
On Error GoTo 0
If Not CellName Is Nothing Then
Application.Goto Reference:=CellName
Else
Application.Goto Reference:=Range("A1")
End If

Thank you.

John Wirt




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Testing for named cells (i.e., ranges)

In essence, you are trying to set an object variable to a string value,
which errors out, but you are not trapping it because of the OnError Next.

This works

Dim CellName As Range

On Error Resume Next
Set CellName = ActiveSheet.Range("anchor")
On Error GoTo 0
If Not CellName Is Nothing Then
Application.Goto Reference:=CellName
Else
Application.Goto Reference:=Range("A1")
End If

if you want to select the whole range, or

Dim CellName As Range

On Error Resume Next
Set CellName = ActiveSheet.Range("anchor")(1, 1)
On Error GoTo 0
If Not CellName Is Nothing Then
Application.Goto Reference:=CellName
Else
Application.Goto Reference:=Range("A1")
End If

if you just want to select the first cell in the range.

Be aware that workbook and worksheet level names can cause some strange
effects. If you want more info, see
http://www.xldynamic.com/source/xld.Names.html


--

HTH

RP
(remove nothere from the email address if mailing direct)


"John Wirt" wrote in message
...
The following code is written to test for the existence of a cell named
"anchor" on the Active Sheet but it does not work. If the named cell

exists,
that named range is supposed to become the active cell.

Something is wrong with the second statement. Even if a cell named

"anchor"
exists on the active sheet, the resulting CellName is Nothing. Why?

Dim CellName as Name

On Error Resume Next
Set CellName = ActiveSheet.Names("anchor").Name
On Error GoTo 0
If Not CellName Is Nothing Then
Application.Goto Reference:=CellName
Else
Application.Goto Reference:=Range("A1")
End If

Thank you.

John Wirt




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
Testing selection against named range [email protected][_2_] Excel Discussion (Misc queries) 5 April 17th 12 04:19 PM
Named Cells (Ranges) Derek Hart Excel Discussion (Misc queries) 4 November 3rd 08 07:59 AM
Replace a spreadsheets named cells/ranges with exact cell address. David McRitchie Excel Discussion (Misc queries) 0 September 28th 05 08:59 PM
Like 123, allow named ranges, and print named ranges WP Excel Discussion (Misc queries) 1 April 8th 05 06:07 PM
named ranges - changing ranges with month selected gr8guy Excel Programming 2 May 28th 04 04:50 AM


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

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"