excel - 按名称重命名多个文件 Excel VBA
问题描述
我对 vba 很陌生,我需要一个 excel 宏,它根据文件名为其分配另一个名称,例如:
aaa12345.txt -----> hello.txt
bb678.txt -----> bye.txt
文件夹中只有两种名称,aaa*.txt 和 bb*.txt
Sub rena_me()
if ffile = Dir("C:\test\aaa*.txt") Then NewName = "yellow.txt"
Name "C:\test\" & ffile As "c:\test\" & NewName
End sub
这段代码是我所拥有的......工作正常,但我不知道如何为这两个文件实现它
解决方案
试试下面的宏...
Option Explicit
Sub rena_me()
Dim myPath As String
Dim myFile As String
Dim newName As String
myPath = "C:\test\"
'check for an aaa file
myFile = Dir(myPath & "aaa*.txt")
If Len(myFile) > 0 Then
Name myPath & myFile As myPath & "hello.txt"
End If
'check for a bb file
myFile = Dir(myPath & "bb*.txt")
If Len(myFile) > 0 Then
Name myPath & myFile As myPath & "bye.txt"
End If
End Sub
推荐阅读
- php - 如何使用 laravel Raw 查询选择行
- microsoft-graph-api - Microsoft Graph .Net 客户端库使用 Group Delta 时是否有延迟?
- javascript - 来自 mysql 数据库的具有多年过滤器的投资组合图像库
- c - 如何使用 MPI_Scatterv 将块大小发送到特定进程?
- asp.net - 开发 Web 项目 + api + SQL Server + dotnet 时出错
- javascript - 为什么 array.filter 返回“ABCDE”和“UHY”
- javascript - 直接更新 useState 对象键?
- python-3.x - 我有一个存储在 hdf5 文件中的大型数组,101 GB,我正在使用 64 GB RAM 的系统,有什么办法可以处理这么大的数据数组吗?
- c# - 如何安装 QuickFix/n
- javascript - vue客户端文件直接从节点js服务器下载不起作用