从Personal.xlsb文件,我希望VBA将当前活动的工作簿另存为用户可定义的名称在同一目录中,并删除原始工作簿。
下面是我的代码。它有两个问题。第一,由于某种原因,它将工作簿保存在“我的文档”文件夹中。活动的工作簿不在“我的文档”中。它位于完全不同的驱动器中的文件夹中。二,它引发“找不到文件”错误。
Sub RenameFile()
Dim thisWb As Workbook
Set thisWb = ActiveWorkbook
MyOldName = ActiveWorkbook.Name
MyNewName = InputBox("What do you want to rename the file as?", "Rename", ActiveWorkbook.Name)
ActiveWorkbook.SaveAs Filename:=thisWb.Path & MyNewName
Kill MyOldName
End Sub
您需要\
在文件名前添加一个after路径。
Sub RenameFile()
Dim thisWb As Workbook
Set thisWb = ActiveWorkbook
MyOldName = ActiveWorkbook.FullName
MyNewName = InputBox("What do you want to rename the file as?", "Rename", ActiveWorkbook.Name)
ActiveWorkbook.SaveAs Filename:=thisWb.Path & "\" & MyNewName
Kill MyOldName
End Sub
编辑:更新了答案以包括来自评论的修复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句