VBA Copy Workbook

VBA Copy Workbook

VBA Copy Workbook is to copy workbook or file using FSO.CopyFile method. Where FSO is nothing but FileSystemObject object. It copies a file from one location to another. We can also create user define function to copy file.

Syntax for Copy Workbook Method

Here is the following syntax to Copy Workbook.

Object.CopyFile(Source As String, Destination As String, [OverWriteFiles As Boolean=True])

Where
Object is a mandatory argument. It always represents FileSystemObject object.
Source is a mandatory argument. It specifies file path to be copy. It represents string data type. The source file path can be relative or absolute. You can include Wildcard characters.
Destination is a required argument. It specifies file path to create. It represents string data type then can’t include Wildcard characters.
OverWriteFiles is an optional argument. It specifies whether file to be overwritten or not. It’s default value is TRUE.

Macro to Copy Workbook using FSO.CopyFile Method

Let us see the following example VBA code to Copy Workbook using FSO.CopyFile Method. It copies Workbook form one location to another location.

'VBA Copy Workbook using FSO.CopyFile in Excel
Sub VBA_Copy_Workbook_FSO()
    
    'Variable declaration
    Dim oFSO As FileSystemObject
    Dim sFilePath As String
    Dim dFilePath As String
    
    'Source & Destination File Path
    sFilePath = "D:\VBAF1\VBA Functionsa.xlsm"
    dFilePath = "D:\Blog\VBA Functionsa.xlsm"
    
    'Reference Workbook by Object named oWorkbook
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    'Copy Workbook
    oFSO.CopyFile sFilePath, dFilePath, True
     
End Sub

Note: If you get any error while running above code, add below specified reference.
VBE->Tools->References->Check the box Microsoft Scripting Runtime

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

Leave a Reply