|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Function CheckFolder(Fso As Scripting.FileSystemObject, Sht As Worksheet) As Folder
- 'Dim oFolder As Folder, oFile As File
- Dim Yyyy, YyyyMm, PathName
- With Sht
- Yyyy = Split(.Name, "年")(0) & "年"
- YyyyMm = Split(.Name, "月")(0) & "月"
- End With
- PathName = ThisWorkbook.Path & "" & Yyyy
-
- If Not Fso.FolderExists(PathName) Then
- Fso.CreateFolder PathName
- End If
- '''
- PathName = ThisWorkbook.Path & "" & Yyyy & "" & YyyyMm
- If Not Fso.FolderExists(PathName) Then
- Fso.CreateFolder PathName
- End If
- PathName = ThisWorkbook.Path & "" & Yyyy & "" & YyyyMm & "" & Sht.Name
- If Not Fso.FolderExists(PathName) Then
- Fso.CreateFolder PathName
- End If
- Set CheckFolder = Fso.GetFolder(PathName)
- End Function
- Private Sub ShtNameToFolder()
- Dim Fso As Scripting.FileSystemObject
- Set Fso = New Scripting.FileSystemObject
- Dim oDate As Date, Str, Arr
- Dim CurrentFolder As Folder, AimFolder As Folder
- Dim oFile As File, oFile1 As File
- Dim Sht As Worksheet, Rng As Range, oRow
- Set Sht = Application.ActiveSheet
- Set AimFolder = CheckFolder(Fso, Sht)
- Set CurrentFolder = Fso.GetFolder(ThisWorkbook.Path)
- 'Debug.Print AimFolder.Path, CurrentFolder.Path
- Dim PathName
- PathName = ThisWorkbook.Path
- Dim FileDict As Scripting.Dictionary
- Set FileDict = New Scripting.Dictionary
- With Sht
- oRow = .Cells(.Rows.Count, 1).End(xlUp).Row
-
- For ii = 4 To oRow
-
-
- If Fso.FolderExists(CurrentFolder.Path) Then
- PathName = CurrentFolder.Path & "" & .Cells(ii, 1)
- If Fso.FileExists(PathName) Then
- Set oFile1 = Fso.GetFile(PathName)
- End If
- PathName = CurrentFolder.Path & "" & .Cells(ii, 2)
- If Fso.FileExists(PathName) Then
- Set oFile = Fso.GetFile(PathName)
- End If
-
- If Not oFile Is Nothing Then
- Set FileDict(oFile) = oFile1
- End If
- End If
- Next ii
- End With
- ''
- With FileDict
- For ii = 0 To .Count - 1
- Debug.Print .Keys(ii).Path, .Items(ii).Path
- Fso.MoveFile .Keys(ii), AimFolder
- Fso.MoveFile .Items(ii), AimFolder
- Next ii
- End With
- End Sub
复制代码
|
|