Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: VBA Code to Rename Folders

  1. #1
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location

    VBA Code to Rename Folders

    Hi All,

    I'm working on a code to rename 1000s of folders and I'm afraid that I've hit a dead end. I'm not entirely sure how to proceed. I have an excel document set up like this:

    Column B: Original File Source
    Column C: New File Source

    How do I code somethng that renames every folder in a parent folder? Any help would be much appreciated. My code is below:

    Sub FolderRename()
        'Declaring variables
    Dim complete_pathof_folder As String, state As String
    For i = 2 To Sheets("Sheet1").Range("B2").End(x1Down).Row
      
      'Variable values
            filesource = Cells(i, 2)
            newfilesource = Cells(i, 6)
           
    'Renames Original Folder Name
    Name completepathof As newfolderpath
    Next i
    End Sub

  2. #2
    what error you are getting using this code?
    A mighty flame followeth a tiny sparkle!!



  3. #3
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location
    I get "Run-time error '1004': Application-defined or object-defined error" message when I run the code--it highlights "For i=2 to Sheets..." when I try to debug it. Any thoughts?

  4. #4
    did you got succes in renaming any of folder? i guess may be folder is not there coz u already renamed it ??
    A mighty flame followeth a tiny sparkle!!



  5. #5
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location
    not with this code, no, but the folder names are not changed

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
       sn = Cells(1, 2).Resize(Cells(Rows.Count, 2).End(xlUp).Row, 2)
       
       For j = 1 To UBound(sn)
          If Dir(sn(j, 1), 16) <> "" and Dir(sn(j, 2), 16) = "" Then Name sn(j, 1) As sn(j, 2)
       Next
    End Sub
    Last edited by snb; 06-19-2015 at 09:29 AM.

  7. #7
    Quote Originally Posted by jflst View Post
    not with this code, no, but the folder names are not changed
    check for special characters in string name..i dont see any error in code...
    A mighty flame followeth a tiny sparkle!!



  8. #8
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location
    For i = 2 To Sheets("Rename File").Range("B2").End(x1Down).Row

    I need to debug this. Any thoughts?

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Hidden Typo. Add "Option Explicit" at the top of your code page and Debug >> Compile will highlight it for you.

    Hint: there is no Lower Case "L" in the following
    .End(x1Down).Row
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location
    now it says I need to define "i" for code line "For i = 2"...How do I define "i"?

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    dim i as Long
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location
    This is what I have now--the message "Compile Error: Variabel not defined" pops up and when I debug it it highlights "(xDown). What's wrong with this? Is the iDim in the right place--and do I need to define variables "filesource" and "newfilesource"?
    Option Explicit
    Sub Folder_Rename1()
        'Declaring variables
    Dim filesource As String
    Dim newfilesource As String
    Dim i As Long
    For i = 2 To Sheets("Rename File").Range("B2").End(xDown).Row
      
      'Variable values
            filesource = Cells(i, 2)
            newfilesource = Cells(i, 6)
           
    'Renames Original Folder Name
    Name filesource As newfilesource
    Next i
    End Sub
      
        'Possible code that works
      'Repeats Code Until an Empty Cell is Reached
            'Do Until IsEmpty(Cells(iRow, 1))
            'dCellValues(iRow) = Cells(iRow, 2).Value
            'iRow = iRow + 1
            'Loop

  13. #13
    You wrote so
    For i = 2 To Sheets("Rename File").Range("B2").End(xDown).Row
    .... and had so
    For i = 2 To Sheets("Rename File").Range("B2").End(xlDown).Row

    (xlDown)

  14. #14
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Syntax error. Change xDown to XLDown.

  15. #15

  16. #16
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    snb's code won't work with Option Explicit, but it will rename all your files really fast. To use it, just comment out the "Option Explicit" line.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  17. #17
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location
    If my excel file is set up as follows:

    Column B: old file source
    Column F: new file source

    Where do I make adjustments to snb's code? And does it rename all files until an empty row?

    Sub M_snb()
        sn = Cells(1, 2).Resize(Cells(Rows.Count, 2).End(xlUp).Row, 2)
         
        For j = 1 To UBound(sn)
            If Dir(sn(j, 1), 16) <> "" And Dir(sn(j, 2), 16) = "" Then Name sn(j, 1) As sn(j, 2)
        Next
    End Sub

  18. #18
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    try to do the first line in his code, then I do the second line.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  19. #19
    VBAX Regular
    Joined
    Jun 2015
    Posts
    10
    Location
    Sub M_snb()

    sn = Cells(2, 2).Resize(Cells(Rows.Count, 2).End(xlUp).Row, 2)

    Cells(2,2) refers to the row, column, which in this cse would be B2. I'm not really sure what the Dir string (second line) does. If I'm correct, it's a directory code but I don't knwo what it does.

  20. #20
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Also your VBEditor has an F1 button : help.

    Sub M_snb() 
        sn = Cells(1, 2).Resize(Cells(Rows.Count, 2).End(xlUp).Row, 5) 
         
        For j = 1 To UBound(sn) 
            If Dir(sn(j, 1), 16) <> "" And Dir(sn(j, 5), 16) = "" Then Name sn(j, 1) As sn(j, 5) 
        Next 
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •