[英]Update the name of Excel Worksheet dynamically- VBA excel
我正在尝试编写一个宏,在其中我可以将工作表的名称等同于单元格值。 到目前为止,我只能提取工作表的名称并将其放入单元格值。
有没有办法可以实现上述目标?
谢谢
已经有解决方案,请点击此链接
感谢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
您可以通过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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.