我正在尝试编写一个宏,我可以将工作表的名称等同于单元格值。 到目前为止,我只能提取工作表的名称并将其放入单元格值。
有没有办法实现上述目标?
由于
答案 0 :(得分:2)
已有解决方案, 请遵循此link
感谢Extendoffice.com的人们
在此处粘贴代码以供参考,只需按照您将找到详细信息的链接
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Target = Range("A1")
If Target = "" Then Exit Sub
Application.ActiveSheet.Name = VBA.Left(Target, 31)
Exit Sub
End Sub
答案 1 :(得分:1)
您可以使用工作表的Worksheet_Change
事件轻松实现此目的。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'note that A1 here is the cell that contains the sheet name. Adjust it to your needs.
If Target.Address(False, False) = "A1" Then
Target.Parent.Name = Target.Text
End If
End Sub
每当A1的值发生变化时,工作表名称都会相应更改。
请注意,此过程必须位于不在模块内的工作表范围内。
对不允许的或空的工作表名称实施错误处理会很有用。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'note that A1 here is the cell that contains the sheet name. Adjust it to your needs.
If Target.Address(False, False) = "A1" And Target.Text <> vbNullString Then
On Error GoTo ERR_NO_RENAME
Target.Parent.Name = Target.Text
On Error GoTo 0
End If
Exit Sub
ERR_NO_RENAME:
If Err Then MsgBox Err.Description, vbCritical, Err.Number, Err.HelpFile, Err.HelpContext
End Sub