2009年9月19日 星期六

檔案分類

今天在excel home看到了人家問 關於檔案歸類的問題
她的寫法其實是用很傳統的迴圈處理方式去判斷新增資料夾還有分類檔案處理
Sub MoveFiles()
'
' MoveFiles Macro
Dim i As Integer
Dim s As String
Dim FilPath As String
Dim aaa As String
Dim bbb As String
Dim MyFile As Object
With Sheet1
For i = 1 To .Range("a65536").End(xlUp).Row
FilPath = ThisWorkbook.Path & "\" & .Cells(i, 1).Text & ".doc"
aaa = ThisWorkbook.Path & "\" & Sheet1.Range("b" & i).Value & "\" & .Cells(i, 1).Text & ".doc"
bbb = ThisWorkbook.Path & "\" & Sheet1.Range("b" & i).Value & ""
If Dir(bbb, vbDirectory) <> "" Then
If Dir(FilPath) <> "" Then
Set MyFile = CreateObject("Scripting.FileSystemObject")
MyFile.MoveFile FilPath, aaa
Set MyFile = Nothing
Else
s = s & Chr(10) & .Cells(i, 1).Text
End If
Else
MkDir (bbb)
Set MyFile = CreateObject("Scripting.FileSystemObject")
MyFile.MoveFile FilPath, aaa
Set MyFile = Nothing
End If
Next
End With
If s <> "" Then
MsgBox s & Chr(10) & "無以上文件!"
End If
End Sub
====================
上面的做法是將檔名寫在a列 分類寫在b類
最後用迴圈分類 移動檔案跟產生資料夾
他還用了FileSystemObject
不過只拿來做資料的移動
有點殺雞用牛刀的感覺
之後有比較厲害的人
寫了下面的語法
Sub MoveFiles()
On Error Resume Next
For Each c In Range([a1], [a65536].End(3))
Name ThisWorkbook.Path & "\" & c.Value & ".doc" As ThisWorkbook.Path & "\" & c(1, 2).Value & "\" & c.Value & ".doc"
Next
End Sub
==================================
這個就高明很多
利用Name陳述式(重新命名一個檔案、目錄、或檔案夾)
Name陳述式可以做到更改檔名,並將檔案搬移至另一個目錄中語法如下
Name OldName As NewName
不過這個做法有個條件
就是預定要移入的資料夾必須先存在

沒有留言:

張貼留言