Excel精英培训网

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

[已解决]求一遍曆指定路徑下將指定後綴名文件移動到當前指定的路徑下的宏代碼,謝謝

[复制链接]
发表于 2013-5-25 10:47 | 显示全部楼层 |阅读模式
比如 我在a1 中輸入d:\a     執行就可將d:\a下所有資料夾及各子資料夾中的BMP圖片文件 移動到 d:\a \圖片\  下此“圖片” 資料夾可判斷沒有就自動創建。謝謝
最佳答案
2013-5-25 11:27
  1. Sub 入口()
  2.     Call ListDirs([d2].Value, "*.bmp")
  3. End Sub
  4. Sub ListDirs(ByVal strPath As String, ByVal strMatch As String)
  5.     Dim strFileName$, strDstFolder$
  6.     Dim arrPath()
  7.     Dim sPath$
  8.     Dim i&, j&

  9.     If Len(strPath) <= 1 Then Exit Sub

  10.     i = 1: j = 1
  11.     If Right(strPath, 1) Like "[/\]" Then strPath = Left(strPath, Len(strPath) - 1)

  12.     On Error Resume Next

  13.     strDstFolder = strPath & Application.PathSeparator & "图片" & Application.PathSeparator
  14.     MkDir strDstFolder

  15.     On Error GoTo ErrorHandler
  16.     ReDim arrPath(1 To 1)

  17.     arrPath(i) = strPath & Application.PathSeparator

  18.     sPath = arrPath(j)

  19.     Debug.Print sPath

  20.     Do While Len(sPath)
  21.         strFileName = Dir(sPath & "*.*", vbDirectory + vbNormal)
  22.         Do While Len(strFileName)

  23.             If Not (strFileName = "." Or strFileName = "..") Then
  24.                 If (GetAttr(sPath & "" & strFileName) And vbDirectory) = 16 Then
  25.                     '避免读取错误
  26.                     If Err.Number <> 0 Then Err.Clear: GoTo End1If
  27.                     If strFileName <> strDstFolder Then
  28.                         i = i + 1
  29.                         ReDim Preserve arrPath(1 To i)
  30.                         arrPath(i) = sPath & strFileName & Application.PathSeparator
  31.                     End If
  32.                 Else
  33.                     If UCase(strFileName) Like UCase(strMatch) Then
  34.                         Name sPath & strFileName As strDstFolder & strFileName
  35.                     End If

  36.                 End If
  37.             End If
  38. End1If:
  39.             strFileName = Dir
  40.         Loop

  41.         j = j + 1
  42.         If j > i Then Exit Do
  43.         sPath = arrPath(j)
  44.     Loop

  45. ErrorHandler:
  46.     MsgBox Err.Number & vbCrLf & _
  47.            Err.Description
  48.     Resume Next

  49. End Sub
复制代码
测试交你了,我这没测。
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-5-25 11:05 | 显示全部楼层
遍历的代码你在论坛可以搜下,很多。

移动的话,就用NAME语句。

评分

参与人数 1 +3 收起 理由
yl.fu + 3 很给力!

查看全部评分

回复

使用道具 举报

发表于 2013-5-25 11:05 | 显示全部楼层
Name 语句
请参阅     示例     特性

重新命名一个文件、目录、或文件夹。

语法

Name oldpathname As newpathname

Name 语句的语法具有以下几个部分:

部分 描述
oldpathname 必要参数。字符串表达式,指定已存在的文件名和位置,可以包含目录或文件夹、以及驱动器。
newpathname 必要参数。字符串表达式,指定新的文件名和位置,可以包含目录或文件夹、以及驱动器。而由 newpathname 所指定的文件名不能存在。



说明

Name 语句重新命名文件并将其移动到一个不同的目录或文件夹中。如有必要,Name 可跨驱动器移动文件。 但当 newpathname 和 oldpathname 都在相同的驱动器中时,只能重新命名已经存在的目录或文件夹。 Name 不能创建新文件、目录或文件夹。

在一个已打开的文件上使用 Name,将会产生错误。必须在改变名称之前,先关闭打开的文件。Name 参数不能包括多字符 (*) 和单字符 (?) 的统配符。

评分

参与人数 1 +3 收起 理由
yl.fu + 3 赞一个! 可是我不懂代碼啊

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-5-25 11:08 | 显示全部楼层
hwc2ycy 发表于 2013-5-25 11:05
Name 语句
请参阅     示例     特性

可是我不懂代碼啊,
回复

使用道具 举报

发表于 2013-5-25 11:27 | 显示全部楼层    本楼为最佳答案   
  1. Sub 入口()
  2.     Call ListDirs([d2].Value, "*.bmp")
  3. End Sub
  4. Sub ListDirs(ByVal strPath As String, ByVal strMatch As String)
  5.     Dim strFileName$, strDstFolder$
  6.     Dim arrPath()
  7.     Dim sPath$
  8.     Dim i&, j&

  9.     If Len(strPath) <= 1 Then Exit Sub

  10.     i = 1: j = 1
  11.     If Right(strPath, 1) Like "[/\]" Then strPath = Left(strPath, Len(strPath) - 1)

  12.     On Error Resume Next

  13.     strDstFolder = strPath & Application.PathSeparator & "图片" & Application.PathSeparator
  14.     MkDir strDstFolder

  15.     On Error GoTo ErrorHandler
  16.     ReDim arrPath(1 To 1)

  17.     arrPath(i) = strPath & Application.PathSeparator

  18.     sPath = arrPath(j)

  19.     Debug.Print sPath

  20.     Do While Len(sPath)
  21.         strFileName = Dir(sPath & "*.*", vbDirectory + vbNormal)
  22.         Do While Len(strFileName)

  23.             If Not (strFileName = "." Or strFileName = "..") Then
  24.                 If (GetAttr(sPath & "" & strFileName) And vbDirectory) = 16 Then
  25.                     '避免读取错误
  26.                     If Err.Number <> 0 Then Err.Clear: GoTo End1If
  27.                     If strFileName <> strDstFolder Then
  28.                         i = i + 1
  29.                         ReDim Preserve arrPath(1 To i)
  30.                         arrPath(i) = sPath & strFileName & Application.PathSeparator
  31.                     End If
  32.                 Else
  33.                     If UCase(strFileName) Like UCase(strMatch) Then
  34.                         Name sPath & strFileName As strDstFolder & strFileName
  35.                     End If

  36.                 End If
  37.             End If
  38. End1If:
  39.             strFileName = Dir
  40.         Loop

  41.         j = j + 1
  42.         If j > i Then Exit Do
  43.         sPath = arrPath(j)
  44.     Loop

  45. ErrorHandler:
  46.     MsgBox Err.Number & vbCrLf & _
  47.            Err.Description
  48.     Resume Next

  49. End Sub
复制代码
测试交你了,我这没测。

评分

参与人数 1 +3 收起 理由
yl.fu + 3 赞一个! 我試試

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-5-25 12:01 | 显示全部楼层
hwc2ycy 发表于 2013-5-25 11:27
测试交你了,我这没测。

你就一牛人{:221:}
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-7 16:22 , Processed in 0.295743 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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