VBA单击按钮创建一个新的工作簿
问题描述:
我正在尝试创建一个宏,该宏在按下现有工作簿上的按钮时会创建一个新工作簿.我想将新创建的工作簿保存在与现有工作簿相同的文件夹中,并给它起一个新的名字?有人可以帮我吗?
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?
非常感谢您!
这是我到目前为止所拥有的代码,但是无法按我想要的方式工作(找不到对象,也没有将其保存在同一文件夹中):
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
答
尝试以下代码:
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: