VBA代码重命名1000s的文件夹 [英] VBA Code to Rename 1000s of Folders

查看:92
本文介绍了VBA代码重命名1000s的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对VBA很新,我正在尝试开发一个重命名文件夹的代码。我有一个Excel工作表设置如下:

 列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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆