使用连接重命名文件名

问题描述 投票:0回答:1

我目前有一个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

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

excel vba filenames
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.