VBA example - Excel 2007: copy files from a folder


VBA code to copy files from a folder

In this VBA code example, you will learn to write VBA code in Excel to copy files from a folder to another folder. By running the VBA code example, all java files in the Myjava folder in drive D are copied to Myjava folder in drive C.
To run this example VBA code, you will need a Form, and a CommandButton.


VBA MS Excel: VBA code to copy files



VBA code for this example:



Option Explicit
Private Sub CommandButton1_Click()

    Dim fs As Variant


    Dim dr As String


    'Create file system object


    Set fs = CreateObject("Scripting.FileSystemObject")


    dr = dir("C:\Myjava", vbDirectory)


    If dr = "" Then 'create the directory if it doesn't exist


         MkDir ("C:\Myjava\")


    End If


    'Copy all java files from Myjava folder in drive D to Myjava folder in drive C and if files exist then override


    fs.CopyFile "D:\Myjava\*.java", "C:\Myjava\", True


   

End Sub

Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.