vba 在excel中复制和重命名未打开的工作簿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9182365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:10:26  来源:igfitidea点击:

Copying and renaming unopened workbook in excel

excelvba

提问by thebiglebowski11

I am trying to copy an entire unopened workbook located at one file path, rename it and save it to a new directory... I am having issues with the renaming and saving, here is my code thus far:

我正在尝试复制位于一个文件路径的整个未打开的工作簿,重命名并将其保存到新目录......我在重命名和保存时遇到问题,这是我的代码:

Private Sub new_file_Click()
Dim ActBook As Workbook, NewFile As String
Dim ToolFilePath As String, DestFilePath As String, NewToolName As String
NewToolName = InputBox("Enter the Tool Name.")

MsgBox (Tools_ListBox.Value)
ToolFilePath = GetToolPath(Tools_ListBox.Value)
ToolFilePath = GetPath & ToolFilePath

DestFilePath = GetPath & GetDestPath(Tools_ListBox.Value) & NewToolName & ".xlsm"

Set ActBook = Workbooks.Add(ToolFilePath)
ActBook.SaveAs DestFilePath
ActBook.Close True

The file that i want to copy is located at the path in ToolFilePath and the located I want to rename it to is DestFilePath, which includes the excel .xlsm extension.

我要复制的文件位于 ToolFilePath 中的路径,我想将其重命名为 DestFilePath,其中包含 excel .xlsm 扩展名。

Where am I going wrong?

我哪里错了?

Thanks

谢谢

回答by Tim Williams

Why not just use:

为什么不使用:

FileCopy(ToolFilePath,DestFilePath)

No need to open it.

没有必要打开它。

回答by Jerry Beaucaire

You can also use the NAME method if you do not wish to retain the original file.

如果您不想保留原始文件,也可以使用 NAME 方法。

Name ToolFilePath As DestFilePath