code not working: Copy file From one directory to another with vba

vba copy all files from one folder to another
excel vba move specific files from one folder to another
vba copy file and rename
access vba move all files from one folder to another
fso.copyfile error handling
vba code in excel to copy source file from sharepoint to another destination
excel vba move file without opening
excel vba copy all files in folder

I have write a VBA code to copy file from one directory to another; but I can't figure out why it is not working. Any ideas? I know how to do it using FileSystemObject but I will like to learn - doing it with SHELL.

Sub copy_file()

    Dim dirPath As String, srcFile As String

    dirPath = "E:\Download\"
    srcFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name

    Shell ("cmd /c copy /y """ & srcFile & " " & dirPath & """")

End Sub

You're over-complicating.

You don't need to use Shell and go through the command line for this.

There's a built-in command: FileCopy


Example

This example uses the FileCopy statement to copy one file to another. For purposes of this example, assume that is a file containing some data.

Dim SourceFile, DestinationFile 
SourceFile = "SRCFILE" ' Define source file name. 
DestinationFile = "DESTFILE" ' Define target file name. 
FileCopy SourceFile, DestinationFile ' Copy source to target. 

Read the documentation for FileCopy at the source.

I'd also suggest taking a few minutes to read through all the standard VBA objects and their available methods/functions/properties/etc to get an idea of what kind of tasks have built-in functionality with VBA and/or Office.

Here is the main page of the official documentation for Office VBA.

Copy Files from One Location to Another Folder Direcory in Excel VBA, If file does not existing in the source location; If file is exists in the Code: Here is the example VBA code to copy the file from one folder to  Here is the another Example to show you different situations while copying file from one location to another folder. Case 1: If file does not existing in the source location. VBA Program to check if file exists or not in the specified location. Case 2: If file is exists in the source location. VBA code should copy the source file to the

srcFile is the same file that you are using to hold your vba code, and therefore that file is already open. In that case Windows shell copy command will not work, by design. You can duplicate your opened source file with the ActiveWorkbook.SaveAs method. Further reading: How to do a "Save As" in vba code, saving my current Excel workbook with datestamp? Further problem: your active workbook will immediately renamed, therefore you have to close it. In order to avoid having save as dialog before exit, read that tutorial: https://support.microsoft.com/en-us/help/213428/how-to-suppress-save-changes-prompt-when-you-close-a-workbook-in-excel

Excel VBA – Copy or Move files from one folder to another, jpg files only, ignoring other types of files. Also Read: Get Data from a Closed Excel File or Workbook without Opening it. The VBA Code. Open an excel file and  Use the CopyDirectory method to copy a directory to another directory. This method copies the contents of the directory as well as the directory itself. If the target directory does not exist, it will be created. If a directory with the same name exists in the target location and overwrite is set to False, the contents of the two directories

Shell ("cmd /c copy /y """ & srcFile & " " & dirPath & """")

Quotes issue. You should be check your path and try this code below:

Sub copy_file()

Dim dirPath As String, srcFile As String

dirPath = "E:\Download\"
srcFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name
MsgBox "cmd /c copy /y " & srcFile & " " & dirPath
Shell ("cmd /c copy /y " & srcFile & " " & dirPath)

End Sub

CopyFile method (Visual Basic for Applications), Documentation · Learn · Code Samples Copies one or more files from one location to another. If True, files are overwritten; if False, they are not. it is assumed that destination is an existing folder in which to copy matching files. Have questions or feedback about Office VBA or this documentation? Copies one or more files from one location to another. CopyFile source, destination, [ overwrite ] The CopyFile method syntax has these parts: Character string file specification, which can include wildcard characters, for one or more files to be copied. Character string destination where the file or files from source are to be copied.

Copy A File From One Folder To Another - Macro, Follow the example and copy the code for free, Home » Copy A File From One Folder To Another – Macro Today I want to show you how to write a simple Excel VBA Macro to copy a file from one location to another, or from a source Why Is My Personal Macro Workbook Not Loading Automatically? Compared to the other, simple solution with FileCopy, this solution seems to have the advantage that the source file can also be copied while it is opened (e.g. by MS Access), i.e. when it's "read-only". – Jörg Brenninkmeyer Dec 1 '17 at 8:51. This method is even easier if you're ok with fewer options: FileCopy source, destination.

VBA Copy a file with FSO CopyFile, Use the VBA CopyFile FileSystemObject (FSO) function to copy a file to into your code when you're accessing a computer's file system. (1) copies one file from one folder to another folder with the VBA To discourage you from doing so​, here's a quick rundown of the problems you may encounter. Try this. It will check to see if the file already exists in the destination folder, and if it does will check if the file is read-only. If the file is read-only it will change it to read-write, replace the file, and make it read-only again. Const DestinationFile = "c:\destfolder\anyfile.txt" Const SourceFile = "c:\sourcefolder\anyfile.txt" Set

Copy and Move files and folders, For one file you can use the VBA Name and FileCopy function and for entire folders or a lot of files use the other macro example's on this page. Note: Read the commented code lines in the code 'Note: If ToPath already exist it will overwrite existing files in this folder 'if ToPath not exist it will be made for you. Dim FSO As  Below are a few examples to copy and move files and folders. For one file you can use the VBA Name and FileCopy function and for entire folders or a lot of files use the other macro example's on this page. Sub Copy_Folder () 'This example copy all files and subfolders from FromPath to ToPath. 'Note: If ToPath already exist it will overwrite

Comments
  • "Not working" - what does it mean? What's happening?
  • Thanks for your reply, but i want to do it with shell command, i already know how to use FileCopy i wanted to know some thing new, your code will help me too.
  • Thanks, i will try it & let you know about it.
  • Thanks for the help, i tried your method but it did not work.
  • Could you please provide detail error info for me ?
  • Did you saved your Excel Worksheet into special place?