Code to move excel file from one folder to another folder

rwilard

New Member
Joined
Nov 29, 2018
Messages
9
Hello, I have been unable to find a code that allows me to move the current workbook from one folder into another so I thought of the idea of a save as that saves in the desired folder but also kills the file in the current folder. I have gotten the save to work but not the kill. The file is present in both folders, can someone help me?

path = "I:\AAA-PURCHASE ORDERS\test1"
path2 = "I:\AAA-PURCHASE ORDERS\test2"
filename1 = "PO#_" & Range("H4").Text & "_" & Range("C10").Text & "_" & Range("H7").Text & "_" & Range("F49").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=path2 & filename1 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
MsgBox "File Saved"
Application.DisplayAlerts = True



Kill path & filename1
Application.ActiveWorkbook.Close False
 
path = "I:\AAA-PURCHASE ORDERS\test1\filename1.xlsm"
path2 = "I:\AAA-PURCHASE ORDERS\test2\filename1.xlsm"
filename1 = "PO#_" & Range("H4").Text & "_" & Range("C10").Text & "_" & Range("H7").Text & "_" & Range("F49").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=path2 & filename1 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
MsgBox "File Saved"
Application.DisplayAlerts = True




Kill path & filename1
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Please read post#3, test the suggestion and report back
 
Upvote 0
Hello, I have been unable to find a code that allows me to move the current workbook from one folder into another so I thought of the idea of a save as that saves in the desired folder but also kills the file in the current folder. I have gotten the save to work but not the kill. The file is present in both folders, can someone help me?

path = "I:\AAA-PURCHASE ORDERS\test1"
path2 = "I:\AAA-PURCHASE ORDERS\test2"
filename1 = "PO#_" & Range("H4").Text & "_" & Range("C10").Text & "_" & Range("H7").Text & "_" & Range("F49").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=path2 & filename1 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
MsgBox "File Saved"
Application.DisplayAlerts = True



Kill path & filename1
Application.ActiveWorkbook.Close False

Try this:
Code:
Public Sub Save_and_Delete()

    Dim saveInPath As String, newFilePath As String
    
    saveInPath = "I:\AAA-PURCHASE ORDERS\test2\"
    
    newFilePath = saveInPath & "PO#_" & Range("H4").text & "_" & Range("C10").text & "_" & Range("H7").text & "_" & Range("F49").text & ".xlsm"
    
    With ThisWorkbook
        Application.DisplayAlerts = False
        .SaveCopyAs Filename:=newFilePath
        Application.DisplayAlerts = True
        .ChangeFileAccess xlReadOnly
        Kill .FullName
        .Saved = True
    End With
    
    MsgBox "Saved " & newFilePath
        
    Application.Quit

End Sub
 
Upvote 0
Wow I have been trying to do this same thing all day long, and finally it works. The last code works, Thanks!
 
Upvote 0
Small change to post 13. Move the .Saved = True line to immediately above the .ChangeFileAccess xlReadOnly line.
 
Last edited:
Upvote 0
Small change to post 13. Move the .Saved = True line to immediately above the .ChangeFileAccess xlReadOnly line.

Nice code John_w

Can you state the reason for your change request for all to know?

Jim
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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