VBA单击按钮创建一个新的工作簿 [英] VBA Create a new workbook with a button click

查看:185
本文介绍了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:

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

这篇关于VBA单击按钮创建一个新的工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆