简体   繁体   English

如何在VBA中自动命名新的Excel工作簿

[英]How to name a new Excel workbook automatically in VBA

I would like to create a New workbook and name it as today's date and DE. 我想创建一个新工作簿并将其命名为今天的日期和DE。 (Eg : 22.01.2018-DE) Format(Date, "dd/mm/yyyy") & "-DE" (例如:22.01.2018-DE) Format(Date, "dd/mm/yyyy") & "-DE"

If the workbook is already existed or opened, then delete it or close it. 如果工作簿已经存在或打开,则将其删除或关闭。 Finally save the workbook. 最后保存工作簿。 I used the code below but not working. 我使用下面的代码,但无法正常工作。 Displaying object defined error . 显示object defined error Help me 帮我

I need to rename Land-DE to 22.01.2018-DE. 我需要将Land-DE重命名为22.01.2018-DE。

Sub createlandDE()
Dim wb As Workbook
Set wb = Workbooks.add
ActiveWorkbook.Names.add Name:=Format(Date, "dd/mm/yyyy") & "-DE"
Dim path As String
Dim FSO As Object
path = "Q:\Job\Land-DE.xlsx" 'Need to rename the file here
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(path) Then
    On Error Resume Next
        Workbooks("Land-DE").Close False 'Workbook name must automatically come here
        Kill path
        wb.SaveAs path
    Else
    wb.SaveAs path
 End If

How about this: 这个怎么样:

Sub createlandDE()
Dim wb As Workbook
Set wb = Workbooks.Add
NameValue = Format(Date, "dd-mm-yyyy") & "-DE"
Dim path As String
Dim FSO As Object
delpath = "Q:\Job\Land-DE.xlsx" 'Need to name the file to delete
path = "Q:\Job\" & NameValue & ".xlsx" 'Need to rename the file here

Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(path) Then
    On Error Resume Next
        Workbooks(NameValue).Close False
        Kill delpath
        wb.SaveAs path
    Else
    wb.SaveAs path
 End If
End Sub

Try with saving as: 尝试另存为:

Workbooks("Land-DE").SaveAs Filename:="Q:\Job\22.01.2018.xlsx"

Then delete the file with the old name. 然后删除具有旧名称的文件。 To make it look better, consider saving "Q:\\Job\\22.01.2018.xlsx" as a String variable. 为了使其看起来更好,请考虑将"Q:\\Job\\22.01.2018.xlsx"为String变量。

I believe you are editing the wrong property. 我相信您在编辑错误的属性。

I would simply use ActiveWorkbook.Name = [your desired name] 我只使用ActiveWorkbook.Name = [您所需的名称]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM