Make New Directory Workbook VBA

Threshold

New Member
Joined
May 26, 2017
Messages
33
Hi All,

So I have this code:

Code:
Sub SaveFilewithNewName()    Dim NewFN As Variant
    Dim NewPth As String
    Dim WRK2 As Workbook
    PostToRegister
    'Copy File to New Path and Filename
    ActiveSheet.Copy
    'Change File Path to Server File Path when ready
    NewPth = "E:\NewProject\File\" & Range("K3")
    MkDir NewPth
    NewFN = NewPth & "\" & Range("K3").Value & ".xlsx"
    ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
    ActiveWorkbook.Close
    NextFile
End Sub

Currently I get the error at MkDir NewPth, NewPth doesn't seem to add the K3 Range... even though when I hover over the name it shows the value for the Range, but doesn't show it as part of NewPth.

Thanks in advance
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
ok, I figured out a little more. It's copying the wrong sheet. It's copying the the file that it's opening. I want it to copy the file that was previously open.
I think that's why I had in the bottom code

DIM WRK2 as Workbook

But I'm not sure how I want to keep it that way...
 
Last edited:
Upvote 0
Try this
Code:
Sub SaveProjectswithNewName()
    Dim NewFN As Variant
    Dim NewPth As String
    Dim Ws As Worksheet
    Set Ws = ActiveWorkbook.Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
    
    PostToRegister
    'Copy Projects to New Path and Filename
    Ws.Copy
    'Change File Path to Server File Path when ready
    NewPth = "E:\Projects\File\" & Ws.Range("K3").Value
    MsgBox NewPth
    MkDir NewPth
    NewFN = NewPth & "\" & Ws.Range("K3").Value & ".xlsx"
    ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
    ActiveWorkbook.Close
    NextFile
End Sub
Change the sheet name in red to suit
 
Upvote 0
Ok I figured it out.
Where it says

Code:
Sub SaveECOwithNewName()    Dim NewFN As Variant
    Dim NewPth As String
    Dim WRK2 As Workbook
    PostToRegister
    'Copy ECO to New Path and Filename
[COLOR=#ff0000]    [/COLOR][COLOR=#008000]ThisWorkbook[/COLOR][COLOR=#ff0000].ActiveSheet.Copy[/COLOR]
    'Change File Path to Server File Path when ready
    NewPth = ("E:\Phil - UKM Projects\ECO\" & Range("K3"))
    MkDir NewPth
    NewFN = NewPth & "\" & Range("K3").Value & ".xlsx"
    ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
    ActiveWorkbook.Close
    NextECO
End Sub

I added the green part. This works for me. I know have a new issue. After it does all that. I want to save the other workbook and close it before it goes to the next part of the code "NextFile" (So the E:\Projects\File\File.XLSM)
 
Upvote 0
Try adding this
Code:
   WBK.Close True
to the end of the PostToregister code
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top