VBA File filtering

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.