使用串联重命名文件名

时间:2019-03-26 07:39:44

标签: excel vba filenames

我目前有一个具有以下输出的excel电子表格

Old_Name               PlayerID        PlayerName
20190324181982.MTS      1               Jake Smith
20190324181963.MTS      2               Greg Johnson
20190324181923.MTS      3               Jake De Maria

“旧名称”列跟踪我上传到计算机的视频文件。我尝试使用VBA代码(请参见下文)来标识存储的文件夹中的“旧名称”,并将其更改为playerid + playername。例如1Jake Smith。

Sub autofilename()
    Dim dca As Workbook
    Dim checklist As Worksheet
    Dim oldfilepath As String
    Dim old_name As String
    Dim playerid As String
    Dim playername As String
    Dim lastrow As Integer

    Set dca = ActiveWorkbook
    Set checklist = dca.Sheets("Venezuela_list")
    lastrow = checklist.Cells(Rows.Count, "A").End(xlUp).Row
    oldfilepath = "C:\Users\nhwal\Docs\Practice"

    For a = 2 To lastrow
        old_name = checklist.Cells(a, 1).Value
        playerid = checklist.Cells(a, 2).Value
        playername = checklist.Cells(a, 3).Value

        On Error Resume Next
        Name old_name & old_name As old_name & playerid & playername & " MTS.url"
    Next a
End Sub

不幸的是,我尝试的代码不会更改文件名。有谁知道如何调整我的代码以使文件更改名称?预先感谢!

1 个答案:

答案 0 :(得分:0)

您不能尝试:

Option Explicit

Sub trst()

    Dim Row As Long, LastRow As Long
    Dim FileLocation As String

    With ThisWorkbook.Worksheets("Sheet1") '<- Change Workbook / Sheet names

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        FileLocation = "C:\Users\nhwal\Docs\Practice\"

        For Row = 2 To LastRow
            Name FileLocation & .Range("A" & Row).Value As FileLocation & .Range("A" & Row).Value & " " & .Range("B" & Row).Value & " " & .Range("C" & Row).Value & " MTS.url"
        Next Row

    End With

End Sub