gpt4 book ai didi

excel - 重命名打开的工作簿而不先关闭它?

转载 作者:行者123 更新时间:2023-12-04 20:01:39 26 4
gpt4 key购买 nike

如果我想重命名一个打开的工作簿,我需要先关闭它,然后重命名它,这有点繁琐。
在第三方软件“Office Tab”上,它有这个功能到Rename opened workbook without firstly close it .
是否可以通过 Excel 完成而不使用其他软件,甚至使用 VBA 灵魂。
提前,非常感谢任何帮助。

最佳答案

重命名打开的工作簿

Option Explicit

Sub RenameMe()

Const ibPrompt As String = "Enter a new file name"
Const ibTitle As String = "Rename Me"

Dim wb As Workbook: Set wb = ActiveWorkbook
Dim DotPosition As Long: DotPosition = InStr(1, wb.Name, ".")
If DotPosition = 0 Then Exit Sub

Dim ibDefault As String: ibDefault = Left(wb.Name, DotPosition - 1)

Dim NewBaseName As String
NewBaseName = InputBox(ibPrompt, ibTitle, ibDefault)
If Len(NewBaseName) = 0 Then Exit Sub

Dim FilePath As String: FilePath = wb.FullName
Dim FolderPath As String: FolderPath = wb.Path & Application.PathSeparator
Dim Extension As String: Extension = Right(Extension, DotPosition)

Dim ErrNum As Long
On Error Resume Next
wb.SaveAs FolderPath & NewBaseName & Extension
ErrNum = Err.Number
On Error GoTo 0

If ErrNum = 0 Then
Kill FilePath
Else
MsgBox "Could not rename.", vbCritical, "Rename Me"
End If

End Sub

关于excel - 重命名打开的工作簿而不先关闭它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71108363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com