【发布时间】:2019-03-26 07:39:44
【问题描述】:
我目前有一个具有以下输出的 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
不幸的是,我尝试的代码并没有改变我的文件名。有谁知道如何调整我的代码以使文件更改名称?提前致谢!
【问题讨论】:
-
什么是
old_name?您正在使用变量old_name, playerid and playername,但您尚未在任何地方声明或设置它们[1] 使用Option Explicit[2] 删除On Error Resume Next并正确处理错误。不必要地使用On Error Resume Next就像告诉应用程序Shut the $%#% up:) -
请注意,行计数变量必须是
Long类型,因为 Excel 的行数超过了Integer可以处理的数量:Dim lastrow As Long -
@SiddharthRout,我认为我从未见过如此简洁地实现
Option Explicit的解释。 -
哎呀,对不起。那是我的错字。我已经使用声明的变量和
dim lastrow进行了适当的编辑,但仍然没有运气 -
为什么是
old_name & old_name而不是oldfilepath & "\" & old_name?代码如何知道文件在哪里?同样适用于old_name & playerid & playername...阅读Name的工作原理...顺便说一句,您仍然拥有On Error Resume Next:D