Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default renaming worksheet with cell contents

I used this code on the worksheet (found it on this site, I think)

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.[o2], Target) Is Nothing Then
On Error GoTo InvalidName
Me.Name = Target.Value
Exit Sub
End If
InvalidName:
MsgBox "Invalid sheet name."
End Sub


which worked great when I did it, renaming the sheet to the content of O2.
I've just opened it today, and although valid names work, and invalid names
call the error message, any change to any other cell also calls the error
message. What's going on?

thanks,
Andy
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default renaming worksheet with cell contents

Because your using the worksheet change event then any change to the
worksheet is calling the macro.

You could put a button on your sheet and use this to call your name change
macro.

"foilfencingandy" wrote:

I used this code on the worksheet (found it on this site, I think)

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.[o2], Target) Is Nothing Then
On Error GoTo InvalidName
Me.Name = Target.Value
Exit Sub
End If
InvalidName:
MsgBox "Invalid sheet name."
End Sub


which worked great when I did it, renaming the sheet to the content of O2.
I've just opened it today, and although valid names work, and invalid names
call the error message, any change to any other cell also calls the error
message. What's going on?

thanks,
Andy

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default renaming worksheet with cell contents

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.[o2], Target) Is Nothing Then
On Error GoTo InvalidName
Me.Name = Target.Value
End If
Exit Sub
InvalidName:
MsgBox "Invalid sheet name."
End Sub

should fix that. You have the exit sub in the wrong place. With it in
your IF statement, if you don't meet the IF condition, you always go through
the error handler.

--
Regards,
Tom Ogilvy



"foilfencingandy" wrote:

I used this code on the worksheet (found it on this site, I think)

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.[o2], Target) Is Nothing Then
On Error GoTo InvalidName
Me.Name = Target.Value
Exit Sub
End If
InvalidName:
MsgBox "Invalid sheet name."
End Sub


which worked great when I did it, renaming the sheet to the content of O2.
I've just opened it today, and although valid names work, and invalid names
call the error message, any change to any other cell also calls the error
message. What's going on?

thanks,
Andy

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default renaming worksheet with cell contents

That's brilliant, thanks. :-)

"Tom Ogilvy" wrote:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.[o2], Target) Is Nothing Then
On Error GoTo InvalidName
Me.Name = Target.Value
End If
Exit Sub
InvalidName:
MsgBox "Invalid sheet name."
End Sub

should fix that. You have the exit sub in the wrong place. With it in
your IF statement, if you don't meet the IF condition, you always go through
the error handler.

--
Regards,
Tom Ogilvy



"foilfencingandy" wrote:

I used this code on the worksheet (found it on this site, I think)

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.[o2], Target) Is Nothing Then
On Error GoTo InvalidName
Me.Name = Target.Value
Exit Sub
End If
InvalidName:
MsgBox "Invalid sheet name."
End Sub


which worked great when I did it, renaming the sheet to the content of O2.
I've just opened it today, and although valid names work, and invalid names
call the error message, any change to any other cell also calls the error
message. What's going on?

thanks,
Andy

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
Refer to a worksheet/name using cell contents? Caeres Excel Worksheet Functions 5 September 23rd 08 03:23 AM
Renaming tabs from particular cell contents scotty Excel Worksheet Functions 5 December 11th 07 05:44 PM
Use of the Contents of a cell as a worksheet name in a function tooMuchFun Excel Worksheet Functions 3 July 3rd 07 04:39 AM
Reflect cell contents into another worksheet Vince Excel Discussion (Misc queries) 1 August 12th 05 06:12 PM
Using contents of cell to refer to a worksheet Mike[_50_] Excel Programming 3 September 24th 03 01:03 AM


All times are GMT +1. The time now is 05:06 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"