现在,文件夹内有一些文件,如图: 我们可以根据自己的需求在Excel表格里将文件结构编写一下,如图: 然后,打开VBA编辑器,加入以下代码 00 Sub main()01 Dim fso02 Dim myPath$03 myPath = ThisWorkbook.Path & '\'04 Set fso = CreateObject('Scripting.FileSystemObject')05 On Error Resume Next06 For i = 1 To [A1].End(xlToRight).Column07 MkDir (myPath & Cells(1, i))08 If Cells(2, i) <> '' Then09 For j = 2 To Cells(65536, i).End(xlUp).Row10 If Dir(myPath & Cells(j, i)) <> '' Then11 'fso.CopyFile myPath & Cells(j, i), myPath & Cells(1, i) & '\''复制12 fso.MoveFile myPath & Cells(j, i), myPath & Cells(1, i) & '\' '移动13 End If14 Next15 End If16 Next17 On Error GoTo 018 Set fso = Nothing19 End Sub 写过这么多期了,估计大家也已经轻车熟路了,所以就不一一讲解了,如果有不明白的地方,可以私信或评论问我,我一定会回答大家。 其中,主要运用了以下几个知识点, 1.MkDir语句:新建目录或文件夹 2.Dir 函数:返回特定文件名(文件夹名),没有找到则返回“” 3.FileSystemObject 对象的MoveFile方法:将一个或多个文件从一个位置移动到另一个位置 4.FileSystemObject 对象的CopyFile方法:将一个或多个文件从一个位置复制到另一个位置 最后,我们来看一下最终效果: |
|
来自: 我的人生宝库 > 《电脑(软件应用)》