如何在文件夹中创build文件夹,文件和保存文件

编辑:用我的问题不够清楚

Sub NewWB2() Dim wb As Workbook Dim POname As String Dim lrow As Long Dim NewfolderPath As String lrow = Cells(Rows.Count, 3).End(xlUp).Row POname = Worksheets("Sheet1").Cells(lrow, 10).Value 'name I want for both the folder and the document MkDir "C:\Users\First.Last\Desktop" & "\" & POname 'creates the folder in the path I want NewfolderPath = "C:\Users\First.Last\Desktop\" & POname ' variable to define that path Set wb = Workbooks.Add("C:\Users\First.Last\Documents\Custom Office Templates\PO Template.xltm") ' creates from template ActiveWorkbook.SaveAs Filename:=POName 'Saves file as variable "POname" End Sub 

这里的一切工作。 我所需要做的就是添加一行代码,将新的工作簿保存在我创build的文件夹中。 我找不到如何做到这一点,不知道如何添加这个。

试试这个,猜你缺less你的文件名:

 ActiveWorkbook.SaveAS Filename:=NewfolderPath & "\" & POname & "\Filename" 

如果你正在使用一个variables为你的文件名尝试:

 ActiveWorkbook.SaveAS Filename:=NewfolderPath & "\" & POname & "\" & Filename 

你能提供一个你正在使用的POname的string例子吗? 我想你错过了NewfolderPath中的一个'\':

尝试:

 Sub NewWB2() Dim wb As Workbook Dim POname As String Dim lrow As Long Dim NewfolderPath As String lrow = Cells(Rows.Count, 3).End(xlUp).Row 'finds the last row POname = Worksheets("Sheet1").Cells(lrow, 10).Value 'name of Folder and File NewfolderPath = "C:\destinationfoldername\" & POname MkDir NewfolderPath 'creates the new folder with the name defined above Set wb = Workbooks.Add("C:\folderwithtemplate\Template.xltm") 'creates new wb from template ActiveWorkbook.SaveAs NewfolderPath & "\" & POname End Sub