Monday, October 22, 2012

How to Copy or Move a File from one Folder to another Folder using Excel VBA Macro

Excel VBA Macro To Copy or Move a File from one location to another location
Sub Copy_One_File()
Dim SourceFilePath As String
Dim TargetFolderPath As String
Dim FSO As Object

Set FSO = CreateObject("Scripting.FileSystemObject")

SourceFile = "C:\Documents and Settings\Administrator\My Documents\SourceFolder\Sample.xlsx"

'File Path Should Specify Exactly With File Extension
TargetFolderPath = "C:\Documents and Settings\Administrator\My Documents\TargetFolder\"

If Right(TargetFolderPath, 1) <> "\" Then
TargetFolderPath = TargetFolderPath & "\"
End If

'Checking the File Existence using FSO Method 
If FSO.FileExists(SourceFile) = False Then
MsgBox ("Source File Does Not Exist in Path")
End If

'Checking the File Existence using DIR Method
If Dir(SourceFile) = vbNullString Then
MsgBox ("Source File Does Not Exist in Path")
End If

On Error Resume Next

FSO.CopyFile SourceFile, TargetFolderPath
MsgBox "SuccessFull Copied"

'FSO.MoveFile SourceFilePath, TargetPath  'To Move
'MsgBox "SuccessFull Moved"

End Sub

Thanks,Tamatam

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog