Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2632|回复: 12

[已解决]有选择地复制不同类型的文件 ...

[复制链接]
发表于 2013-9-6 18:29 | 显示全部楼层 |阅读模式
有选择地复制不同类型的文件.zip (45 KB, 下载次数: 14)
 楼主| 发表于 2013-9-6 20:23 | 显示全部楼层
回复

使用道具 举报

发表于 2013-9-6 20:56 | 显示全部楼层    本楼为最佳答案   
  1. Dim clg As Collection

  2. Sub FilesCopy()
  3.     Dim strPathSrc As String
  4.     Dim strPathDest As String
  5.     Dim l As Long
  6.     Dim strFilename As String
  7.    
  8.     Set clg = New Collection
  9.     strPathSrc = ThisWorkbook.Path & Application.PathSeparator & "文件"
  10.     strPathDest = ThisWorkbook.Path & Application.PathSeparator & "复制(模拟的结果)" & Application.PathSeparator
  11.     Call fso(strPathSrc, "*.xls")
  12.     For l = 1 To clg.Count
  13.         strFilename = clg.Item(l)
  14.         FileCopy strFilename, strPathDest & Mid(strFilename, InStrRev(strFilename, "") + 1)
  15.     Next
  16. End Sub
  17. Sub fso(ByVal sPath As String, ByVal strPatterm As String)
  18.     Dim fs As Object
  19.     Dim fd As Object
  20.     Dim fc As Object
  21.     Dim s As Object, d As Object
  22.     Set fs = CreateObject("scripting.filesystemobject")
  23.     Set fd = fs.getfolder(sPath)
  24.     Set fc = fd.Files
  25.     For Each s In fc
  26.         If UCase(s.Name) Like UCase(strPatterm) And s.Name <> ThisWorkbook.Name Then clg.Add s.Path
  27.     Next
  28.     For Each d In fd.SubFolders
  29.         Call fso(d.Path, strPatterm)
  30.     Next
  31. End Sub
复制代码

评分

参与人数 1 +3 收起 理由
松儿 + 3

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-9-6 21:03 | 显示全部楼层
hwc2ycy 发表于 2013-9-6 20:56

请增加一个复制 到自动生成的文件夹中,自动生成的文件夹名为“复制”。谢谢!
回复

使用道具 举报

发表于 2013-9-6 21:11 | 显示全部楼层
  1. Sub FilesCopy()
  2.     Dim strPathSrc As String
  3.     Dim strPathDest As String
  4.     Dim l As Long
  5.     Dim strFilename As String

  6.     Set clg = New Collection
  7.     strPathSrc = ThisWorkbook.Path & Application.PathSeparator & "文件"
  8.     strPathDest = ThisWorkbook.Path & Application.PathSeparator & "复制" & Application.PathSeparator
  9.     On Error Resume Next
  10.     MkDir strPathDest
  11.     Call fso(strPathSrc, "*.xls")
  12.     For l = 1 To clg.Count
  13.         strFilename = clg.Item(l)
  14.         FileCopy strFilename, strPathDest & Mid(strFilename, InStrRev(strFilename, "") + 1)
  15.     Next
  16. End Sub

  17. Sub fso(ByVal sPath As String, ByVal strPatterm As String)
  18.     Dim fs As Object
  19.     Dim fd As Object
  20.     Dim fc As Object
  21.     Dim s As Object, d As Object
  22.     Set fs = CreateObject("scripting.filesystemobject")
  23.     Set fd = fs.getfolder(sPath)
  24.     Set fc = fd.Files
  25.     For Each s In fc
  26.         If UCase(s.Name) Like UCase(strPatterm) And s.Name <> ThisWorkbook.Name Then clg.Add s.Path
  27.     Next
  28.     For Each d In fd.SubFolders
  29.         Call fso(d.Path, strPatterm)
  30.     Next
  31. End Sub
复制代码

评分

参与人数 1 +3 收起 理由
松儿 + 3 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-9-6 21:23 | 显示全部楼层
hwc2ycy 发表于 2013-9-6 21:11

没有复制到文件。
回复

使用道具 举报

 楼主| 发表于 2013-9-6 21:25 | 显示全部楼层
hwc2ycy 发表于 2013-9-6 21:11

生成了文件夹,但没有复制到文件。
回复

使用道具 举报

发表于 2013-9-6 21:54 | 显示全部楼层
松儿 发表于 2013-9-6 21:25
生成了文件夹,但没有复制到文件。

我这测试复制了耶。
回复

使用道具 举报

发表于 2013-9-6 21:55 | 显示全部楼层
我删除了原有的复制文件夹,再运行后,还是有文件复制成功了。

评分

参与人数 1 +3 收起 理由
松儿 + 3 非常感谢您的耐心帮助

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-9-6 22:14 | 显示全部楼层
hwc2ycy 发表于 2013-9-6 21:55
我删除了原有的复制文件夹,再运行后,还是有文件复制成功了。

请给个附件。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-19 12:22 , Processed in 0.354595 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表