wrote :: 2005.07.17

Private Sub UserForm_Initialize()
Dim wb, ws
With TreeView1
.Indentation = 14
.LabelEdit = tvwManual
.BorderStyle = ccNone
.HideSelection = False
.LineStyle = tvwRootLines
.ImageList = ImageList1
For Each wb In Workbooks
.Nodes.Add(Key:=wb.Name, Text:=wb.Name, Image:="book").Expanded = True
For Each ws In wb.Worksheets
.Nodes.Add Relative:=wb.Name, Relationship:=tvwChild, _
Key:=wb.Name & "!" & ws.Name, Text:=ws.Name, Image:="sheet"
Next ws
Next wb
End With
End Sub

.Nodes.Add(Key:=wb.Name, Text:=wb.Name, Image:="book").Expanded = True
.Nodes.Add Relative:=wb.Name, Relationship:=tvwChild, _
Key:=wb.Name & "!" & ws.Name, Text:=ws.Name, Image:="sheet"
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Dim BookName As String, SheetName As String
If Node.Children = 0 Then
BookName = Node.Parent
SheetName = Node
TextBox1 = Workbooks(BookName).Worksheets(SheetName).UsedRange.Address
Else
TextBox1 = ""
End If
End Sub

Private Sub CommandButton1_Click()
Dim BookName As String, SheetName As String
With TreeView1
If .SelectedItem.Children > 0 Then ''[*1]
MsgBox "削除するシートを選択してください", vbExclamation
Exit Sub
End If
BookName = .SelectedItem.Parent ''[*2]
SheetName = .SelectedItem
If MsgBox(SheetName & " を削除しますか?", 36) = vbYes Then ''[*3]
Application.DisplayAlerts = False
Workbooks(BookName).Worksheets(SheetName).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

Private Sub UserForm_Initialize()
With TreeView1
.Indentation = 14
.LabelEdit = tvwManual
.BorderStyle = ccNone
.HideSelection = False
.LineStyle = tvwRootLines
.ImageList = ImageList1
End With
Call ResetTreeView
End Sub
Private Sub CommandButton1_Click()
Dim BookName As String, SheetName As String
With TreeView1
If .SelectedItem.Children > 0 Then
MsgBox "削除するシートを選択してください", vbExclamation
Exit Sub
End If
BookName = .SelectedItem.Parent
SheetName = .SelectedItem
If MsgBox(SheetName & " を削除しますか?", 36) = vbYes Then
Application.DisplayAlerts = False
Workbooks(BookName).Worksheets(SheetName).Delete
Application.DisplayAlerts = True
End If
End With
Call ResetTreeView
End Sub
Private Sub ResetTreeView()
Dim wb, ws
With TreeView1
.Nodes.Clear
For Each wb In Workbooks
.Nodes.Add(Key:=wb.Name, Text:=wb.Name, Image:="book").Expanded = True
For Each ws In wb.Worksheets
.Nodes.Add Relative:=wb.Name, Relationship:=tvwChild, _
Key:=wb.Name & "!" & ws.Name, Text:=ws.Name, Image:="sheet"
Next ws
Next wb
.Nodes(1).Selected = True
TextBox1 = ""
End With
End Sub
Private Sub TreeView1_KeyUp(KeyCode As Integer, ByVal Shift As Integer)
If KeyCode = 113 Then
TreeView1.StartLabelEdit
End If
End Sub
Private Sub TreeView1_AfterLabelEdit(Cancel As Integer, NewString As String)
Dim BookName As String, SheetName As String, ws, flag As Boolean
With TreeView1
BookName = .SelectedItem.Parent
SheetName = .SelectedItem
''同じ名前のシートが存在するか?
For Each ws In Workbooks(BookName).Worksheets
If ws.Name = NewString Then
flag = True
Exit For
End If
Next ws
If flag Then
MsgBox NewString & "はすでに存在します", vbExclamation
Cancel = True
Exit Sub
End If
''実際のリネーム処理
On Error Resume Next
Workbooks(BookName).Worksheets(SheetName).Name = NewString
''リネームが成功したか?
For Each ws In Workbooks(BookName).Worksheets
If ws.Name = NewString Then
flag = True
Exit For
End If
Next ws
''失敗したら元に戻す
If Not flag Then
MsgBox NewString & "はシート名に指定できません", vbExclamation
Cancel = True
End If
End With
End Sub
