Excel精英培训网

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

[已解决]关于改名运行

[复制链接]
发表于 2014-12-15 15:30 | 显示全部楼层 |阅读模式
Sub rename()
For i = 1 To Sheets.Count
Sheets(i).Name = Sheets(i).Cells(4,1).Value
Next
End Sub
以上代码实现工作表以第4行第1Cells(4, 1)命名

我目标是:运行时选择文件夹  批量选择文件 改名 完成关闭
请高手 指点



最佳答案
2014-12-15 16:44
本帖最后由 suye1010 于 2014-12-15 18:10 编辑
  1. Dim EAPP,TEMPwb,i,Shell,ObjectPath,FolderPath,FSO,FSOFolder,FSOFile
  2. Set EAPP=CreateObject("Excel.Application")
  3. Set Shell = CreateObject("Shell.Application")
  4. Set ObjectPath = Shell.BrowseForFolder(0, "请选择工作表的所在的文件夹", 0, 0)
  5. If ObjectPath Is Nothing Then
  6. Wscript.Quit
  7. End If
  8. FolderPath=ObjectPath.Self.Path
  9. Set FSO = CreateObject("Scripting.FileSystemObject")
  10. Set FSOFolder = FSO.GetFolder(FolderPath)
  11. For Each FSOFile In FSOFolder.Files
  12. Set TEMPwb=EAPP.Workbooks.Open(FSOFile)
  13. For i=1 to TEMPwb.Worksheets.Count
  14.         TEMPwb.Sheets(i).Name=TEMPwb.Sheets(i).Cells(4,1).Value
  15.         TEMPWB.Close True
  16. Next
  17. Next
  18. Msgbox "已完成"&FolderPath&"文件夹下工作表改名"
  19. EAPP.Quit
复制代码
复制粘贴到记事本,另存为.vbs文件,双击运行即可
发表于 2014-12-15 16:03 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2014-12-15 16:23 | 显示全部楼层
对  不过要改的 文件有点多   所以需要   
运行时 手动先选文件夹  批量选文件 然后处理 然后保存 然后关闭 这样一个过程
回复

使用道具 举报

发表于 2014-12-15 16:23 | 显示全部楼层
你是要改工作簿的名字还是说每个工作簿里面的工作表的名字?你这个只能更改活动工作簿里面的每一个工作表的名字。
回复

使用道具 举报

 楼主| 发表于 2014-12-15 16:30 | 显示全部楼层
工作簿 不改名 只改 工作表   
回复

使用道具 举报

发表于 2014-12-15 16:44 | 显示全部楼层    本楼为最佳答案   

VBS+FileSystemObject

本帖最后由 suye1010 于 2014-12-15 18:10 编辑
  1. Dim EAPP,TEMPwb,i,Shell,ObjectPath,FolderPath,FSO,FSOFolder,FSOFile
  2. Set EAPP=CreateObject("Excel.Application")
  3. Set Shell = CreateObject("Shell.Application")
  4. Set ObjectPath = Shell.BrowseForFolder(0, "请选择工作表的所在的文件夹", 0, 0)
  5. If ObjectPath Is Nothing Then
  6. Wscript.Quit
  7. End If
  8. FolderPath=ObjectPath.Self.Path
  9. Set FSO = CreateObject("Scripting.FileSystemObject")
  10. Set FSOFolder = FSO.GetFolder(FolderPath)
  11. For Each FSOFile In FSOFolder.Files
  12. Set TEMPwb=EAPP.Workbooks.Open(FSOFile)
  13. For i=1 to TEMPwb.Worksheets.Count
  14.         TEMPwb.Sheets(i).Name=TEMPwb.Sheets(i).Cells(4,1).Value
  15.         TEMPWB.Close True
  16. Next
  17. Next
  18. Msgbox "已完成"&FolderPath&"文件夹下工作表改名"
  19. EAPP.Quit
复制代码
复制粘贴到记事本,另存为.vbs文件,双击运行即可
回复

使用道具 举报

 楼主| 发表于 2014-12-15 17:49 | 显示全部楼层
楼上的不行啊
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-16 10:21 , Processed in 0.263586 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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