LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Named range in workseet code

Hi Patrick,
If the code is in a normal module, yes.
But if it's in a worksheet module, no.
Dave Peterson's answer explains this well.
Regards - Dave.

"Patrick Molloy" wrote:

excel 2003
new workbook with three sheets
select sheet2
name a cell TEST and give it a value
select sheet1
in code

Sub check()
MsgBox Range("TEST")
End Sub

this works irrespective of which sheet it active
though I'm sure it shouldn't.

"Dave" wrote in message
...
Hi Jacob,
I tried 'With ThisWorkbook.Names("CompNames")'
and it also works, without sloving the mystery of why
'With [CompNames]' works, but 'With Range("CompNames")' doesn't.
Thanks for your input.
Regards - Dave.

"Jacob Skaria" wrote:

Dave; try

Private Sub Worksheet_Activate()
With ThisWorkbook.Names("CompNames")
MsgBox .RefersTo
End With
End Sub

or use the Workbook SheetActivate Event

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
With Range("CompNames")
MsgBox .Address(External:=True)
End With
End Sub


--
Jacob


"Dave" wrote:

Hi
I am using XL2003
This is a follow-on from a previous post.
I have the following (meaningless) Worksheet Window macro:

Private Sub Worksheet_Activate()
With Range("CompNames")
End With
End Sub

If the named range (CompNames) is on the sheet being activated, the
code
runs ok.
If the named range is on a sheet other than the one being activated, I
get
the following error:

Runtime error '1004'
Application-defined or object-defined error

If the code is put into a normal macro in a module, the error does not
occur.

Sub Test()
With Range("CompNames")
End With
End Sub

I have discovered that I can get around this by using:
Private Sub Worksheet_Activate()
With [CompNames]
End With
End Sub

But I can't find any reference to this style of notation in the Help,
nor do
I know why it works when the other doesn't, or if there are any
limitations
of this syntax. Someone in another group mentioned it, and I tried it
out of
desperation.
Needless to say, I have a slightly more useful macro in mind, but I
have
thinned it down to this to try and solve the thing.
Has anyone else had this problem, or can anyone else confirm that this
is
so? It's been driving me nuts.

Regards - Dave.


.

 
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
VBA code to affect value of a named range Dave O Excel Discussion (Misc queries) 5 August 25th 06 10:12 PM
Problem getting the value of a Named Range in code tbone[_2_] Excel Programming 1 March 21st 06 02:20 AM
Named ranges vs setting range in code Tim Excel Programming 2 February 24th 06 02:50 AM
How to conditional color a named range in VBA code mark engineer Excel Programming 1 October 31st 05 03:12 AM
Named Range Fails in VBA Code Dean Hinson[_3_] Excel Programming 3 January 24th 05 03:48 PM


All times are GMT +1. The time now is 06:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"