简体   繁体   中英

How to Create Folder, File, and Save File in Folder

EDIT: Wasn't clear enough with my question

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

Everything here works. All I need to do is to add a line of code that will save the new workbook in the folder I've created. I can't find how to do this and don't know how to add this in.

Can you provide a string example of POname that you are using? I think you missed a '\\' in NewfolderPath:

Try:

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

Try with this, guess you are missing your Filename:

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

if you are using a Variable for your filename try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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