Excel精英培训网

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

[已解决]批量将整个文件夹中的excel表格的sheet1重命名

[复制链接]
发表于 2016-12-18 22:05 | 显示全部楼层 |阅读模式
1、一个文件夹内有很多个excel表格,每一个表格中有一个工作簿名(sheet1),需要批量将整个文件夹中的excel表格的sheet1重命名为每个excel表格的A2内容。请问用vba如何操作?2、一个文件夹内有很多个excel表格,每一个表格中有一个工作簿名(汉字名),需要批量将整个文件夹中的excel表格的(汉字名)重名称为sheet1,如果有多个,则这个excel表格的第二个sheet2为。请问用vba如何操作?
3、请问两个vba执行,怎么合成一个执行?

最佳答案
2016-12-19 19:00
本帖最后由 7171 于 2016-12-22 10:19 编辑

Sub t()
'把代码放到新的Excel文件中运行

  strPath = ThisWorkbook.Path & Application.PathSeparator
  strFilename = Dir(strPath & "*.xls*")
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  On Error Resume Next
  Do While Len(strFilename)
    If strFilename <> ThisWorkbook.Name Then
      With GetObject(strPath & strFilename)     
        .Windows(1).Visible = True
        .Worksheets(1).Name = .Worksheets(1).Range("A2")  
        .SaveAs Filename:=strPath & .Worksheets(1).Range("A2")  
        .Close    '
        Kill strPath & strFilename      
      End With
    End If
    strFilename = Dir
  Loop

End Sub
已修改,试试

样表.rar

11.67 KB, 下载次数: 15

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2016-12-19 19:00 | 显示全部楼层    本楼为最佳答案   
本帖最后由 7171 于 2016-12-22 10:19 编辑

Sub t()
'把代码放到新的Excel文件中运行

  strPath = ThisWorkbook.Path & Application.PathSeparator
  strFilename = Dir(strPath & "*.xls*")
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  On Error Resume Next
  Do While Len(strFilename)
    If strFilename <> ThisWorkbook.Name Then
      With GetObject(strPath & strFilename)     
        .Windows(1).Visible = True
        .Worksheets(1).Name = .Worksheets(1).Range("A2")  
        .SaveAs Filename:=strPath & .Worksheets(1).Range("A2")  
        .Close    '
        Kill strPath & strFilename      
      End With
    End If
    strFilename = Dir
  Loop

End Sub
已修改,试试
回复

使用道具 举报

 楼主| 发表于 2016-12-19 22:58 | 显示全部楼层
7171 发表于 2016-12-19 19:00
Sub t()
'把代码放到新的Excel文件中运行

您好,为什么运行完毕以后,excel表格打开为灰色,里面没有内容,谢谢!
回复

使用道具 举报

发表于 2016-12-20 11:54 | 显示全部楼层
本帖最后由 7171 于 2016-12-20 12:19 编辑
xiaolin1113 发表于 2016-12-19 22:58
您好,为什么运行完毕以后,excel表格打开为灰色,里面没有内容,谢谢!

'.Windows(1).Visible = True  此句前面多了个  '删除即可
回复

使用道具 举报

 楼主| 发表于 2016-12-28 17:12 | 显示全部楼层
7171 发表于 2016-12-19 19:00
Sub t()
'把代码放到新的Excel文件中运行

真的可以,太感谢了!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 15:56 , Processed in 0.969200 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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