I am writing a VBA code to pull the file at the source folder and move to my folder to analyze it in order not to corrupt the original file. But, i am facing difficulties to write a code that replace the older file if detect the name is the same.
Private Sub CommandButton1_Click()
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
Dim datasource As Workbook
Dim target As Worksheet
Dim strName As String
Dim lastrow As Long
Dim userinput As String
userinput = InputBox(Prompt:="Please key in the file name and format", Title:=" File name", Default:=".xlsm") 'This is Your File Name which you want to Copy
sSFolder = "J:\Inter Dept\MP8 Packaging\2.0 MP8.1\B2-Machine Data Tracking\B2-Machine Data Tracking 2019\" 'Change to match the source folder path
sDFolder = "C:\Users\limmigmy\Desktop\Rejection Report\Rejection\Packing Analysis\Production Files\" 'Change to match the destination folder path
Set FSO = CreateObject("Scripting.FileSystemObject") 'Create Object
If Not FSO.FileExists(sSFolder & userinput) Then 'Checking If File Is Located in the Source Folder
MsgBox "Specified File Not Found", vbInformation, "Not Found"
ElseIf Not FSO.FileExists(sDFolder & userinput) Then 'Copying If the Same File is Not Located in the Destination Folder
FSO.CopyFile (sSFolder & sFile), sDFolder, True
MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
**Else
MsgBox "Specified File Already Exists In The Destination Folder",** vbExclamation, "File Already Exists"
End If