使用VBA重命名文件夹中的文件名

时间:2019-11-27 16:47:01

标签: excel vba

我试图列出文件夹中特定文件类型的名称,然后在相邻列中输入新名称,然后使用VBA重命名。

我有提取文件名的代码:


Public Sub ListSIM()

Dim strFolder, FileType, full As String

strFolder = Range("b1") & "\"

strPattern = Range("b2")



Dim strFile As String

strFile = Dir(strFolder & strPattern, vbNormal)

Sheets("CreateList").Select

Range("d2").Select



Do While Len(strFile) > 0

    ActiveCell.Value = strFile

    ActiveCell.Offset(1, 0).Select

    strFile = Dir()

Loop

End Sub

我正在努力用代码将文件重命名为E列中的新名称,特别是xRow不包含值。


Sub RenamePictures()



Dim xDir As String

Dim xFile As String

Dim xRow As Long



strFolder = Range("b1") & "\"

strPattern = Range("b2")



xFile = Dir(strFolder & strPattern, vbNormal)

    Do Until Len(xFile) < 0

        xRow = 0

        On Error Resume Next

        xRow = Application.Match(xFile, Range("d:d"), 0)

        If xRow > 0 Then

            Name xDir & Application.PathSeparator & xFile As _

            xDir & Application.PathSeparator & Cells(xRow, "E").Value & ".jpeg"

        End If

        xFile = Dir

    Loop



End Sub

0 个答案:

没有答案