I'd use
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Range)
on error resume next
sh.Name = sh.Range("a2").Value
if err.number <> 0 then
msgbox "Rename failed"
err.clear
end if
on error goto 0
End Sub
But I'm not sure I'd do this each time the selection changed. If A2 was
changing because of the user typing something in that cell, I'd use the
workbook_sheetchange event. If it changed because of a calculation, I'd use the
Workbook_SheetCalculate event.
Kind of like one of these -- and only use one!
Option Explicit
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Application.EnableEvents = False
On Error Resume Next
Sh.Name = Sh.Range("a2").Value
If Err.Number <> 0 Then
MsgBox "Rename failed"
Err.Clear
End If
On Error GoTo 0
Application.EnableEvents = True
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Intersect(Target, Sh.Range("a2")) Is Nothing Then
Exit Sub
End If
On Error Resume Next
Sh.Name = Sh.Range("a2").Value
If Err.Number <> 0 Then
MsgBox "Rename failed"
Err.Clear
End If
On Error GoTo 0
End Sub