LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Update sheetname from cell, automatically

It sounds like you got something you can work with!

Good to hear.

wrote:

Thank you ever so much. I can't believe how helpful you have been. You
are both amazing and patient to stick with me for the last 10 days. I
wish there was some way to reward you for your time.

On Jan 10, 8:30 am, Dave Peterson wrote:
Maybe something like:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

Const sERROR As String = "Invalid worksheet name in cell "

Dim sSheetName As String
Dim mySheet As Object

With Target

'only one cell at a time
If .Cells.Count 1 Then Exit Sub

Select Case Target.Address(0, 0)
Case Is = "A3": Set mySheet = Sheet2
Case Is = "A4": Set mySheet = Sheet3
Case Is = "A5": Set mySheet = Sheet4
'I'm too lazy...
Case Is = "A36": Set mySheet = Sheet26
Case Else
Exit Sub
End Select

sSheetName = .Value 'or .text if you have it formatted nicely

If Not sSheetName = "" Then
On Error Resume Next
mySheet.Name = sSheetName
If Err.Number < 0 Then
MsgBox sERROR & .Address(0, 0)
End If
On Error GoTo 0
End If
End With
End Sub

Make sure you use A3, not a3. The comparison is case sensitive.

wrote:

Sorry to be so unclear in my last post. This is what I was talking
about. It works absolute perfectly its just alot of code


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)


Const sTOURCELL1 As String = "A3"
Const sTOURCELL2 As String = "A4"
Const sTOURCELL3 As String = "A5"
Const sTOURCELL4 As String = "A6"
Const sTOURCELL5 As String = "A7"
Const sTOURCELL6 As String = "A8"
Const sTOURCELL7 As String = "A9"
Const sTOURCELL8 As String = "A10"
Const sTOURCELL9 As String = "A11"
Const sTOURCELL10 As String = "A12"
Const sTOURCELL11 As String = "A13"
Const sTOURCELL12 As String = "A14"
Const sTOURCELL13 As String = "A15"
Const sTOURCELL14 As String = "A16"
Const sTOURCELL15 As String = "A17"
Const sTOURCELL16 As String = "A18"
Const sPLAYERNAMECELL1 As String = "A22"
Const sPLAYERNAMECELL2 As String = "A23"
Const sPLAYERNAMECELL3 As String = "A24"
Const sPLAYERNAMECELL4 As String = "A25"
Const sPLAYERNAMECELL5 As String = "A26"
Const sPLAYERNAMECELL6 As String = "A27"
Const sPLAYERNAMECELL7 As String = "A28"
Const sPLAYERNAMECELL8 As String = "A29"
Const sPLAYERNAMECELL9 As String = "A30"
Const sPLAYERNAMECELL10 As String = "A31"
Const sPLAYERNAMECELL11 As String = "A32"
Const sPLAYERNAMECELL12 As String = "A33"
Const sPLAYERNAMECELL13 As String = "A34"
Const sPLAYERNAMECELL14 As String = "A35"
Const sPLAYERNAMECELL15 As String = "A36"
Const sPLAYERNUMBERCELL1 As String = "B22"
Const sPLAYERNUMBERCELL2 As String = "B23"
Const sPLAYERNUMBERCELL3 As String = "B24"
Const sPLAYERNUMBERCELL4 As String = "B25"
Const sPLAYERNUMBERCELL5 As String = "B26"
Const sPLAYERNUMBERCELL6 As String = "B27"
Const sPLAYERNUMBERCELL7 As String = "B28"
Const sPLAYERNUMBERCELL8 As String = "B29"
Const sPLAYERNUMBERCELL9 As String = "B30"
Const sPLAYERNUMBERCELL10 As String = "B31"
Const sPLAYERNUMBERCELL11 As String = "B32"
Const sPLAYERNUMBERCELL12 As String = "B33"
Const sPLAYERNUMBERCELL13 As String = "B34"
Const sPLAYERNUMBERCELL14 As String = "B35"
Const sPLAYERNUMBERCELL15 As String = "B36"


Const sERROR As String = "Invalid worksheet name in cell "


Dim sSheetName As String
Dim mySheet As Object


With Target


'only one cell at a time
If .Cells.Count 1 Then Exit Sub


If Not (Intersect(.Cells, Me.Range(sTOURCELL1)) Is Nothing)
Then
'in A3
Set mySheet = Sheet2
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL2)) Is
Nothing) Then
'in A4
Set mySheet = Sheet3
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL3)) Is
Nothing) Then
'in A5
Set mySheet = Sheet4
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL4)) Is
Nothing) Then
'in A6
Set mySheet = Sheet5
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL5)) Is
Nothing) Then
'in A7
Set mySheet = Sheet6
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL6)) Is
Nothing) Then
'in A8
Set mySheet = Sheet7
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL7)) Is
Nothing) Then
'in A9
Set mySheet = Sheet8
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL8)) Is
Nothing) Then
'in A10
Set mySheet = Sheet9
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL9)) Is
Nothing) Then
'in A11
Set mySheet = Sheet10
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL10)) Is
Nothing) Then
'in A12
Set mySheet = Sheet11
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL11)) Is
Nothing) Then
'in A13
Set mySheet = Sheet12
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL12)) Is
Nothing) Then
'in A14
Set mySheet = Sheet13
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL13)) Is
Nothing) Then
'in A15
Set mySheet = Sheet14
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL14)) Is
Nothing) Then
'in A16
Set mySheet = Sheet35
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL15)) Is
Nothing) Then
'in A17
Set mySheet = Sheet36
ElseIf Not (Intersect(.Cells, Me.Range(sTOURCELL16)) Is
Nothing) Then
'in A18
Set mySheet = Sheet37
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL1)) Is
Nothing) Then
'in A22
Set mySheet = Sheet15
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL2)) Is
Nothing) Then
'in A23
Set mySheet = Sheet16
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL3)) Is
Nothing) Then
'in A24
Set mySheet = Sheet17
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL4)) Is
Nothing) Then
'in A25
Set mySheet = Sheet18
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL5)) Is
Nothing) Then
'in A26
Set mySheet = Sheet19
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL6)) Is
Nothing) Then
'in A27
Set mySheet = Sheet20
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL7)) Is
Nothing) Then
'in A28
Set mySheet = Sheet21
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL8)) Is
Nothing) Then
'in A29
Set mySheet = Sheet22
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL9)) Is
Nothing) Then
'in A30
Set mySheet = Sheet23
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL10)) Is
Nothing) Then
'in A31
Set mySheet = Sheet24
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL11)) Is
Nothing) Then
'in A32
Set mySheet = Sheet25
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL12)) Is
Nothing) Then
'in A33
Set mySheet = Sheet38
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL13)) Is
Nothing) Then
'in A34
Set mySheet = Sheet39
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL14)) Is
Nothing) Then
'in A35
Set mySheet = Sheet40
ElseIf Not (Intersect(.Cells, Me.Range(sPLAYERCELL15)) Is
Nothing) Then
'in A36
Set mySheet = Sheet26
Else
'not in either cell
Exit Sub
End If


sSheetName = .Value 'or .text if you have it formatted nicely


If Not sSheetName = "" Then
On Error Resume Next
mySheet.Name = sSheetName
If Err.Number < 0 Then
MsgBox sERROR & .Address(0, 0)
End If
On Error GoTo 0
End If
End With
End Sub


On Jan 8, 9:32 am, Dave Peterson wrote:
First, thank you for top-posting. That's exactly what I meant.


I'm not quite sure I understand the question, though.


If you want a quick way to go to different worksheets, you could use a toolbar
that allows you to select the one you want.


Here's a macro from Debra Dalgleish's site:
http://contextures.com/xlToolbar01.html

As for renaming sheets, I'd drop the worksheet_change event and use a dedicated
macro.


You could base it on the sheet number (reading the tabs from the left to the
right) or the codename. I'm not sure of the layout of your workbook, though.


wrote:


This is really great. Thanks for your help and thanks for the advice
on top posting except I can't figure out how to top post. I was using
Firefox and going on goggle newsgroups for the posting. I can't find
any settings anywhere for top posting unless putting this at the top
instead of the bottom is what you mean.


I figured out what the error was I didn't make all of the changes
correctly. I am not using dates as tab names only text. I have created
an excel workbook for keeping track of volley stats. I have tabs for
16 tournaments and 15 players. Each tournament name and player name
and number are entered on the setup page. To make it easier to go to
the correct tournament or player tab I have created hyperlinks. I also
wanted to automatically update the sheet name so a user could quickly
go to a tournament or player by clicking on a tab that had the name
of the tournament or player. I know that I can create 21 constants and
1 If Not and 20 ElseIf Not sections but I'm sure that their must be a
better way. I just stated learning visual basic last week. I have just
learned since my first post that you can use a constant just once, And
the difference in sheets vs. sheet. You have been really helpful.


On Jan 7, 7:47 am, Dave Peterson wrote:
For the first problem, you'll have to share what you typed into A3. My bet is
that you may think that it's valid, but excel KNOWS that it's not. (Is it a
date or time? They can cause problems.)


Second, you only get one of those events. You have to combine the code into one
procedu


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)


Const sNAMECELL1 As String = "A3"
Const sNAMECELL2 As String = "A4"
Const sERROR As String = "Invalid worksheet name in cell "


Dim sSheetName As String
Dim mySheet As Object


With Target


'only one cell at a time
If .Cells.Count 1 Then Exit Sub


If Not (Intersect(.Cells, Me.Range(sNAMECELL1)) Is Nothing) Then
'in A3
Set mySheet = Sheet2
ElseIf Not (Intersect(.Cells, Me.Range(sNAMECELL2)) Is Nothing) Then
'in A4
Set mySheet = Sheet3
Else
'not in either cell
Exit Sub
End If


sSheetName = .Value 'or .text if you have it formatted nicely


If Not sSheetName = "" Then
On Error Resume Next
mySheet.Name = sSheetName
If Err.Number < 0 Then
MsgBox sERROR & .Address(0, 0)
End If
On Error GoTo 0
End If
End With
End Sub


If you are using a date or time, but you have the cell formatted nicely (no
slashes, no backslashes, no colons, no anything bad), then you could use .text
instead of .value in the rename statement.


ps.


You may have noticed that most regulars are top posters. (Yes, it's different
than most newsgroups.) Personally, I find it easier to follow the thread when
people top post.


You may want to conform to what most of the regulars do.


wrote:


<<snipped
Dave thanks this work great except for a couple of things. The first
time I ran the code it worked perfect. Now when I make a change in A3
I get a message Invalid worksheet name in cell A3 but the name is
changed correctly.


second I tried copy the code to a second line and changed A3 to A4 and
changed sheet2.name to sheet3.name. When I change cell A4 I get a
compilation error Ambiguous name detected: Worksheet_Change. What am I
doing wrong. Here is a copy of the code


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Const sNAMECELL As String = "A3"
Const sERROR As String = "Invalid worksheet name in cell "
Dim sSheetName As String


With Target


If Not Intersect(.Cells, Range(sNAMECELL)) Is Nothing Then
sSheetName = Range(sNAMECELL).Value
If Not sSheetName = "" Then
On Error Resume Next
Sheet2.Name = sSheetName
On Error GoTo 0
If Not sSheetName = Sheet1.Name Then _
MsgBox sERROR & sNAMECELL
End If
End If
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Const sNAMECELL As String = "A4"
Const sERROR As String = "Invalid worksheet name in cell "
Dim sSheetName As String


With Target


If Not Intersect(.Cells, Range(sNAMECELL)) Is Nothing Then
sSheetName = Range(sNAMECELL).Value
If Not sSheetName = "" Then
On Error Resume Next
Sheet3.Name = sSheetName
On Error GoTo 0
If Not sSheetName = Sheet1.Name Then _
MsgBox sERROR & sNAMECELL
End If
End If
End With
End Sub


--


Dave Peterson


--


Dave Peterson


--

Dave Peterson


--

Dave Peterson
 
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
Cell update - Automatically Apoorva Excel Discussion (Misc queries) 1 June 12th 08 11:39 AM
Automatically update SheetName in workbook sub if SheetName changes [email protected] Excel Discussion (Misc queries) 3 February 29th 08 04:33 PM
Automatically update a cell Adriana Excel Worksheet Functions 2 June 8th 07 04:23 AM
Automatically update cell value Kaitlin.uk Excel Discussion (Misc queries) 2 February 12th 07 11:57 AM
Referenced cell does not automatically update wannaboardx Excel Worksheet Functions 3 July 26th 05 08:34 PM


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