VBA之文件筛选
在工作中,經常會碰到從一堆腐朽的source中按照一個列表去篩選出來現在還要用的source文件。
這個如果用vba來實現的話,會節省大量的時間,而且不會出錯。
前提說明:
將想要復制的文件名列表放在第一sheet的第一列,然后執行程序
首先選擇源目錄和目標目錄, 然后會從源目錄中查找文件,將存在的文件自動復制的目標目錄中,
不存在的文件,記錄在第二列里。
Sub fileFilter()Dim folderOld As StringDim folderNew As StringDim fileNm As StringDim fileNmOld As StringDim fileNmNew As StringDim i As IntegerDim j As Integerj = 2MsgBox "Set before moving folder"With Application.FileDialog(msoFileDialogFolderPicker)If .Show = -1 ThenfolderOld = .SelectedItems(1)End IfEnd WithMsgBox "Set after moving folder"With Application.FileDialog(msoFileDialogFolderPicker)If .Show = -1 ThenfolderNew = .SelectedItems(1)End IfEnd WithFor i = 1 To 1000fileNm = Worksheets(1).Cells(i, 1)If fileNm <> "" ThenfileNmOld = folderOld & "\" & fileNmfileNmNew = folderNew & "\" & fileNmIf Dir(fileNmOld) <> "" ThenFileCopy fileNmOld, fileNmNewElseWorksheets(1).Cells(j, 2) = fileNmj = j + 1End IfEnd IfNextMsgBox "file filter over"End Sub?
另外,vba讀取文件方法備用
Sub readFile()Dim txtLineDim FileObjDim TextObjDim FilePathWith Application.FileDialog(msoFileDialogFilePicker)If .Show = -1 ThenFilePath = .SelectedItems(1)End IfEnd WithDim txt As StringOpen FilePath For Input As #1Do While Not EOF(1)Line Input #1, txtMsgBox txtLoopClose #1End Sub?
轉載于:https://www.cnblogs.com/changxinblog/p/5361762.html
總結
 
                            
                        - 上一篇: oracle性能优化之awr分析
- 下一篇: PHP MySQL 数据字典生成器
