Excel精英培训网

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

Excel 原文档名存在不能被识别的字符(chr(160)),如何批量重命名去除

[复制链接]
发表于 2023-2-1 16:12 | 显示全部楼层 |阅读模式
Excel 原文件名存在不能被识别的字符(chr(160)),使用  name as 重命名文件,提示文件名错误  (就是原文档名存在chr(160)字符,无法被vba读取),批量重命名如何处理?(附件为文件示例及代码)

附件.rar

9.8 KB, 下载次数: 5

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2023-2-2 08:55 | 显示全部楼层
回复

使用道具 举报

发表于 2023-2-2 12:03 | 显示全部楼层
用正则去处理,但是挺奇怪的,测试\s能匹配上,但是替换无效果,太久没用正则都不会用了……

Sub 批量重命名含空格文件()
Dim MyPath, MyName, rege As Object, mh, ss, str$
Application.ScreenUpdating = False
MyPath = InputBox("请输入要重命名的文件路径", "HI", "c:\") & "\"
MyName = Dir(MyPath & "\*.*")
Set rege = CreateObject("vbscript.regexp")
If MyPath = "" Then Exit Sub
Do While MyName <> ""
With rege
    .Global = True
    .Pattern = "[一-龢]+|\..*"
    Set mh = .Execute(MyName)
    For Each ss In mh
        str = str & ss
    Next ss
    MyName2 = str
    str = ""
End With
Name MyPath & MyName As MyPath & MyName2
MyName = Dir()
Loop
End Sub
回复

使用道具 举报

发表于 2023-2-2 14:05 | 显示全部楼层
  1. Sub demo()
  2.   Application.ScreenUpdating = False
  3.   Set fso = CreateObject("scripting.filesystemobject")
  4.   Set re = CreateObject("vbscript.regexp")
  5.   MyPath = InputBox("Hello", "HI", "c:\demo\附件")
  6.   For Each file In fso.GetFolder(MyPath).Files
  7.      re.Pattern = "[\u00A0]"
  8.      MyName = re.Replace(file.Name, "")
  9.      file.Name = MyName
  10.   Next
  11. End Sub
复制代码


demo.zip

22.93 KB, 下载次数: 0

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 03:27 , Processed in 0.287092 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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