In the work, often encounter from a bunch of rotten source in a list to filter out now also use the source file.
This can save a lot of time if it is implemented in VBA, and it will not go wrong.
Prerequisite Description:
Place the list of file names you want to copy in the first column of the first sheet, and then execute the program
First select the source directory and the destination directory, and then locate the files from the source directory, automatically copy the existing files to the destination directory,
Files that do not exist are recorded in the second column.
SubFileFilter ()DimFolderold as String DimFoldernew as String DimFilenm as String DimFilenmold as String DimFilenmnew as String DimI as Integer DimJ as IntegerJ=2 MsgBox "Set before moving folder" withApplication.filedialog (msoFileDialogFolderPicker)If. Show =-1 ThenFolderold= . SelectedItems (1) End If End with MsgBox "Set After moving folder" withApplication.filedialog (msoFileDialogFolderPicker)If. Show =-1 Thenfoldernew= . SelectedItems (1) End If End with fori =1 to +Filenm= Worksheets (1). Cells (I,1) IfFilenm <>"" ThenFilenmold= Folderold &"\"&Filenm filenmnew= Foldernew &"\"&FilenmIf Dir(Filenmold) <>"" Then FileCopyFilenmold, FilenmnewElseWorksheets (1). Cells (J,2) =Filenm J= j +1 End If End If Next MsgBox "file filter over" End Sub
In addition, VBA reads the file method alternate
SubReadFile ()DimTxtlineDimFileobjDimTextobjDimFilePath withApplication.filedialog (msoFileDialogFilePicker)If. Show =-1 ThenFilePath= . SelectedItems (1) End If End with DimTxt as StringOpen FilePath for Input as#1 Do while not EOF(1) LineInput#1, txtMsgBoxtxtLoopClose #1End Sub
VBA File filtering