VBA 通过单击按钮创建一个新工作簿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47308051/
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-08 10:55:25  来源:igfitidea点击:

VBA Create a new workbook with a button click

excelvba

提问by Rulli Smith

I'm trying to create a macro that creates a new workbook when pressing a button on the already existing workbook. I would like to save the newly created workbook in the same folder as the exiting workbook and give it a new name? Could somebody help me please?

我正在尝试创建一个宏,当按下现有工作簿上的按钮时,该宏会创建一个新工作簿。我想将新创建的工作簿保存在与现有工作簿相同的文件夹中并为其指定一个新名称?有人可以帮我吗?

Thank you very much in advance!

非常感谢您提前!

That's the code I have until now but it doesn't work as I want it (object not found and doesn't save it in the same folder):

这是我到现在为止的代码,但它不能按我想要的方式工作(未找到对象并且未将其保存在同一文件夹中):

Sub CreateNewWorkBook()

'Adding New Workbook
Workbooks.Add
'Saving the Workbook
 ActiveWorkbook.SaveAs Filename:=thisWb.Path & "\Test.xls"
 ActiveWorkbook.Close savechanges:=False

End Sub

回答by

Try the following code from:

尝试以下代码:

https://msdn.microsoft.com/en-us/library/office/aa221273(v=office.11).aspx

https://msdn.microsoft.com/en-us/library/office/aa221273(v=office.11​​).aspx

Sub AddNew()
Set NewBook = Workbooks.Add
    With NewBook
        .Title = "All Sales" 'You can modify this value.
        .Subject = "Sales" 'You can modify this value.
        .SaveAs Filename:="Allsales.xls"
    End With
End Sub

To get the path of the file there is already a question about that:

要获取文件的路径,已经有一个问题:

How to get the path of current worksheet in VBA?

如何在VBA中获取当前工作表的路径?