excel - VBA通过添加空格重命名

标签 excel vba

我想在文档名称之间添加一个空格。但是,代码无法通过 IF True 行。

文档名称具有随机名称,如下:

INV500TAT1234 Rev 1 Form_Health.pdf

希望它是:

INV500 TAT1234 Rev 1 Form_Health.pdf #with a space in between 500 & TAT

请注意,某些文档已经包含 INV500 TAT,因此我编写了 if 来跳过它们

Sub Rename_Space()
    Dim ffile As Variant, path As String
    path = "C:\Users\me\Desktop\Rename\"
    ffile = Dir(path)
    While ffile <> ""
        If ffile = "INV500TAT*" Then
            old_name = path & ffile
            new_name = path & "INV500 TAT*"
            Name old_name As new_name
        End If
        ffile = Dir
    Wend
End Sub

最佳答案

这将在每个以 INV500TAT 开头的文件名中的 500TAT 之间留出一个空格。

Sub Rename_Space()
    Dim ffile As Variant, path As String
    path = "C:\Users\me\Desktop\Rename\"
    ffile = Dir(path)
    While ffile <> ""
    Debug.Print ffile
        If Left(ffile, 9) = "INV500TAT" Then
        old_name = path & ffile
        new_name = path & Left(ffile, 6) & " " & Mid(ffile, 7, Len(ffile))
        Name old_name As new_name
        End If
        ffile = Dir
    Wend
End Sub

关于excel - VBA通过添加空格重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65715270/

相关文章:

ms-access - MS Access VBA ADODB Recordset.打开表正常但 SQL 失败

excel - 如何避免电子邮件自动化的 Outlook 安全警告?

excel - 为什么 VBA copytoClipboard 不再工作?

查找包含单元格的一组区域的算法

excel - 定位图表

删除工作表后不再声明 VBA 全局变量

vba - 在 Excel 中拆分大写单词并插入分隔符

vba - 如何在VBA中对变量范围内的值求和?

jQuery:将表导出到 Excel 时如何在文件上添加名称

excel - 为什么第一个随机数总是一样的?