如何用VBA快速修改文件名

如何用VBA快速修改文件名,第1张

Sub 批量改名()

  Dim FolderName As String, wbName As String, cValue As Variant

  Dim wbList() As String, wbCount As Integer, i As Integer, str As String, exname As String

  FolderName = "G:\360data\重要数据\桌面\新建文件夹"   '文件夹路径

  '创建文件夹中工作簿列表

  wbCount = 0

  wbName = Dir(FolderName & "\" & "*.xls*")

  While wbName <> ""

    wbCount = wbCount + 1

    ReDim Preserve wbList(1 To wbCount)

    wbList(wbCount) = wbName

    wbName = Dir

  Wend

  If wbCount = 0 Then Exit Sub

  '从每个工作簿中获取数据

  For i = 1 To wbCount

    cValue = GetInfoFromClosedFile(FolderName, wbList(i), "sheet1", "a1")

  exname = Mid(wbList(i), InStr(wbList(i), "."))

  Name FolderName & "\" & wbList(i) As FolderName & "\" & cValue & exname

  On Error Resume Next

  Name FolderName & "\" & wbList(i) As FolderName & "\" & cValue & i & exname

  Next i

End Sub

'====================从未打开表中获取信息===========================

Private Function GetInfoFromClosedFile(ByVal wbPath As String, _

    wbName As String, wsName As String, cellRef As String) As Variant

  Dim arg As String

  GetInfoFromClosedFile = ""

  If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"

  If Dir(wbPath & "\" & wbName) = "" Then Exit Function

  arg = "'" & wbPath & "[" & wbName & "]" & _

        wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)

  r = 0

  On Error Resume Next

  GetInfoFromClosedFile = ExecuteExcel4Macro(arg)

End Function

有一个批处理的:

@echo off

echo 正在替换文件名,请稍等......

ren AA.xls BB.xls

#说明:把AA文件名替换成BB文件名

echo 替换文件名完成!

echo. &pause

另存为.bat,执行替换即可。ren语句可以增加,在excel中做好,另存为txt,再存为bat即可,注意空格。

在当前目录下,dos命令:dir/b>1.txt 可以获得文件名

按照下列步骤可实现:

1、在表的B1单元格输入="COPY test " &A1,然后把光标移动到B1单元格右下角,当光标变成实心黑十字时,双击左键,实现表格自动填充;

2、选中B列全部内容,进行复制

3、打开记事本,将复制内容全部粘贴到新的文本文件中,然后在最前面添加一行,内容为@ECHO OFF

4、将文本文件存储到与test文件夹同一个路径,存储为BAT文件,文件名随意,只是要注意编码选ANSI

5、双击该脚本文件即可


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8067089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存