我收到了一个 Excel 文件,其中文件名是从文件夹插入的,它将其转换为新名称,如果有重复项,我会使用不同的后续编号重命名它们。但是当我尝试重写文件夹中的文件名时,我收到一条错误消息。
Sub RenameFiles()
Dim myPath As String
myPath = "/Users/kenny/Documents/Foto/"
r = 2
Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 2))
Name myPath & Cells(r, 1).Value As myPath & Cells(r, 2).Value
r = r + 1
Loop
End Sub
它在“Name myPath & Cells(r, 1).Value As myPath & Cells(r, 2).Value”行上给出错误
一些额外的信息:我从装有 excel 2016 的 Windows 计算机上得到了这个(我认为),我有一台装有 Office 365 excel 的 Mac。
我尝试调整一些东西,但对 VBA 来说是新的。
我找到了一个新代码来解决该错误。显然这是因为特殊字母、符号,如 é、è 等。 我写了这段代码。
子重命名文件()
Dim myPath As String
Dim r As Long
Dim oldName As String
Dim newName As String
myPath = "/Users/kenny/Documents/Foto/"
r = 2
On Error GoTo ErrorHandler ' Error handling inschakelen
Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 2))
oldName = myPath & Cells(r, 1).Value
newName = myPath & Cells(r, 2).Value
' Controleer of het doelbestand al bestaat
If Dir(newName) <> "" Then
MsgBox "Bestand " & newName & " bestaat al. Rij: " & r
r = r + 1
GoTo NextIteration
End If
' Probeer het bestand te hernoemen
Name oldName As newName
NextIteration:
r = r + 1
Loop
MsgBox "Alle bestanden zijn succesvol hernoemd."
Exit Sub
ErrorHandler:
MsgBox "Er is een fout opgetreden bij het hernoemen van bestand in rij " & r & ". Fout: " & Err.Description
Resume NextIteration
结束子
但现在我需要一些带有特殊符号的文件名。我如何合并这些,以便它们也会在同一个脚本中发生变化?