Excel精英培训网

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

[已解决]VB批量重命名~~求助各位大大们~

[复制链接]
发表于 2014-6-20 18:29 | 显示全部楼层 |阅读模式
在同一文件中,有“广东-广州-A-1.csv”、“广东-深圳-B-2.csv”、“广东-珠海-C-3.csv”、等多个文件,用VB批量重命名为“广东1.csv”、“广东2.csv”、“广东3csv”等,向各位老师,大虾请教!!其中文件名中的“-*-*-”为变量,不一定是上述字符,求代码。万分感激~~~~
最佳答案
2014-6-20 19:43
  1. Sub demo1()
  2.     Dim objDic As Object
  3.     Dim strPath$, strFile$, strTemp$
  4.     Set objDic = CreateObject("scripting.dictionary")
  5.     strPath = ThisWorkbook.Path & ""
  6.     strFile = Dir(strPath & "*.xls")
  7.     Do While Len(strFile)
  8.         If strFile <> ThisWorkbook.Name Then
  9.             If strFile Like "*-*-*-*.*" Then
  10.                 strTemp = Left(strFile, InStr(strFile, "-") - 1)
  11.                 objDic(strTemp) = objDic(strTemp) + 1
  12.                 Name strPath & strFile As strPath & strTemp & objDic(strTemp) & Mid(strFile, InStrRev(strFile, "."))
  13.             End If
  14.         End If
  15.         strFile = Dir
  16.     Loop
  17.     Set objDic = Nothing
  18. End Sub
复制代码
没有测试,另外,如果重复运行,应该会产生错误,而且有BUG。
发表于 2014-6-20 19:43 | 显示全部楼层    本楼为最佳答案   
  1. Sub demo1()
  2.     Dim objDic As Object
  3.     Dim strPath$, strFile$, strTemp$
  4.     Set objDic = CreateObject("scripting.dictionary")
  5.     strPath = ThisWorkbook.Path & ""
  6.     strFile = Dir(strPath & "*.xls")
  7.     Do While Len(strFile)
  8.         If strFile <> ThisWorkbook.Name Then
  9.             If strFile Like "*-*-*-*.*" Then
  10.                 strTemp = Left(strFile, InStr(strFile, "-") - 1)
  11.                 objDic(strTemp) = objDic(strTemp) + 1
  12.                 Name strPath & strFile As strPath & strTemp & objDic(strTemp) & Mid(strFile, InStrRev(strFile, "."))
  13.             End If
  14.         End If
  15.         strFile = Dir
  16.     Loop
  17.     Set objDic = Nothing
  18. End Sub
复制代码
没有测试,另外,如果重复运行,应该会产生错误,而且有BUG。
回复

使用道具 举报

 楼主| 发表于 2014-6-20 20:19 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-11 11:42 , Processed in 0.212682 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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