VBA代码重命名1000s的文件夹 [英] VBA Code to Rename 1000s of Folders
问题描述
列A:文件夹路径
列B:旧名称
列C:新名称
这是一个我发现在一个单一的夹。我不知道它是否可能扩展这段代码(如循环),或者我需要开发一个全新的代码。任何帮助将不胜感激。
Sub rename_folder()
Dim old_name,new_name As String
For i = 2 To Sheets(1).Range(a1)。End(xlDown).Row
new_name = Left(Sheets(1).Cells(i,1).Value,Len(Sheets(1) .Cells(i,1).Value) - Len(Sheets(1).Cells(i,2).Value))
new_name = new_name& (1).Cells(i,3).Value
old_name = Sheets(1).Cells(i,1).Value
Name old_name As new_name
Next i
End Sub
我有点困惑你的问题。你说你显示的代码已经在一个文件夹中工作了...我看不到该代码可以对文件夹进行任何更改,只能在Excel中操作数据。
但是,我清理了代码:
Sub rename_folder()
Dim old_name,new_name As String
With Sheets(1)
For i = 2 To .Range(a1)。End(xlDown).Row
new_name = Left(.Cells(i,1)) Value,Len(.Cells(i,1).Value) - Len(.Cells(i,2).Value))
new_name = new_name& .Cells(i,3).Value
old_name = .Cells(i,1).Value
名称old_name作为new_name
下一个i
结束
End Sub
I'm very new to VBA and I'm trying to develop a code that renames folders. I have a worksheet in Excel set up like this:
Column A: Path of Folder
Column B: Old Name
Column C: New Name
This is a code that I found which works on a single folder. I'm not sure if its possible to make an extension of this code (such as a loop) or if I need to develop a whole new code. Any help would be much appreciated.
Sub rename_folder()
Dim old_name, new_name As String
For i = 2 To Sheets(1).Range("a1").End(xlDown).Row
new_name = Left(Sheets(1).Cells(i, 1).Value, Len(Sheets(1).Cells(i, 1).Value) - Len(Sheets(1).Cells(i, 2).Value))
new_name = new_name & Sheets(1).Cells(i, 3).Value
old_name = Sheets(1).Cells(i, 1).Value
Name old_name As new_name
Next i
End Sub
I'm a bit confused over your question. You said the code you showed already worked for one folder... I don't see how that code could make any changes to a folder... it only manipulates data in Excel.
However, I cleaned up the code a bit:
Sub rename_folder()
Dim old_name, new_name As String
With Sheets(1)
For i = 2 To .Range("a1").End(xlDown).Row
new_name = Left(.Cells(i, 1).Value, Len(.Cells(i, 1).Value) - Len(.Cells(i, 2).Value))
new_name = new_name & .Cells(i, 3).Value
old_name = .Cells(i, 1).Value
Name old_name As new_name
Next i
End With
End Sub
这篇关于VBA代码重命名1000s的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!