我正在编写一个 VBA 代码来提取源文件夹中的文件并移动到我的文件夹进行分析,以免损坏原始文件。但是,如果检测到名称相同,我将面临编写替换旧文件的代码的困难。
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