Okay, I decided it was too easy for you to screw up your worksheet if all
you implemented was what you requested, so I tried to develop a more
"complete" solution for you... and I almost have it. I **think** I have
covered all possibilities except one (more about that in a moment). The
following code should allow you to add, delete and modify (one at a time
only, and only by typing into the cell in the specified range) the names
in your list and have the proper action take place with the referenced
worksheets (for example, if you erase a name, and answer Yes to the
question that is asked, the name will be erased and the worksheet
deleted). I believe I have covered all of the possibilities (if not, let
me know and I'll try patch the code) with the exception of
dragging/dropping text from a **single** cell (I have multiple cells
covered) into the range specified (in the Const statement at the start of
the code)... there seems to be no way to detect drag-and-drop editing, or
at least not that I have been able to find, so I don't know how to stop a
user from doing that. (If anyone knows of a method, I would like to hear
about it.) So, with that single exception, the code below should (I hope)
give you a fairly complete "editor" for the range of cells containing your
worksheet names. Give it a try and let me know.
'*************** START OF CODE ***************
Dim PreviousEntry As String
Dim MultipleSelection As Boolean
Const NameRange As String = "A2:A20"
Private Sub Worksheet_Activate()
If Not Intersect(ActiveCell, Range(NameRange)) Is Nothing Then
PreviousEntry = ActiveCell.Value
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim X As Long
Dim WS As Worksheet
Dim SheetName As String
Dim Answer As Long
If Not Intersect(Target, Range(NameRange)) Is Nothing Then
If MultipleSelection Then
MsgBox "You can only change worksheet names one-at-a-time!"
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
ElseIf Application.CutCopyMode <> False Then
MsgBox "You can only change worksheet names by typing!"
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
ElseIf Len(Trim(Target.Value)) = "" Or IsEmpty(Target.Value) Then
Answer = MsgBox("STOP!!! Deleting this name will DELETE the" & _
"associated worksheet ('" & PreviousEntry & _
"') meaning ALL data on it will be lost!" & _
vbNewLine & vbNewLine & _
"Do you still want to erase this name?", _
vbCritical Or vbYesNo Or vbDefaultButton2)
If Answer = vbYes Then
Application.DisplayAlerts = False
Worksheets(PreviousEntry).Delete
Application.DisplayAlerts = True
PreviousEntry = ""
Else
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
End If
ElseIf Not IsFileName(Target.Value) Then
MsgBox "That is not a valid worksheet name!"
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
ElseIf Len(Trim(PreviousEntry)) > 0 Then
For Each WS In Worksheets
If WS.Name = PreviousEntry Then
WS.Name = Target.Value
PreviousEntry = Target.Value
Exit For
End If
Next
ElseIf Len(PreviousEntry) = 0 Then
PreviousEntry = Target.Value
For X = Range(NameRange).Row To Target.Row - 1
If Len(Trim(Cells(X, Range(NameRange).Column).Value)) > 0 Then
SheetName = Cells(X, Range(NameRange).Column).Value
End If
Next
If Len(SheetName) > 0 Then
Worksheets.Add After:=Worksheets(SheetName)
Else
For X = Target.Row + 1 To Range(NameRange).Row +
Range(NameRange).Count
If Len(Trim(Cells(X, Range(NameRange).Column).Value)) > 0 Then
SheetName = Cells(X, Range(NameRange).Column).Value
Exit For
End If
Next
If Len(SheetName) > 0 Then
Worksheets.Add Before:=Worksheets(SheetName)
Else
Worksheets.Add After:=Worksheets(Worksheets.Count)
End If
End If
ActiveSheet.Name = Target.Value
Target.Parent.Select
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MultipleSelection = (Target.Count > 1)
If Not MultipleSelection Then PreviousEntry = Target.Value
End Sub
Function IsFileName(StringIn As String) As Boolean
If Len(StringIn) > 255 Or StringIn Like "*[*?<>""/\|:']*" Or _
InStr(1, "*CON*AUX*COM1*COM2*COM3" & _
"*COM4*LPT1*LPT2*LPT3*PRN*NUL*", _
"*" & StringIn & "*", vbTextCompare) Then
IsFileName = False
Else
IsFileName = True
End If
End Function
'*************** END OF CODE ***************
Rick
Chris said:
Hello
On my first worksheet there is a list of names. I would like these names
to
come in the tab of the following worksheets because for every person in
the
list there is a seperate worksheet with data and calculations.
i.e. worksheet with list of names = LIST. 2nd worksheet=John, 3rd
worksheet=Mary, according to the list on the LIST-worksheet.
Is this possible?
Thank you for any kind of help.
Chris