+ Reply to Thread
Results 1 to 5 of 5

Rename folders using values from sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-06-2007
    Posts
    70

    Question Rename folders using values from sheet

    Hi all,

    I have a list of reference numbers in column A, and an address in column B.

    Along with that - i have a folder which contains loads of folders named with the reference numbers. Is there a way using VBA, that i can go through each cell in column A and perform the following:

    Go through each reference number in column A, check if there is a folder with that name, if so - rename with the corresponding value in column B.

    I hope i have explained this correctly

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Rename folders using values from sheet

    You can try with this code:
    Sub Macro1()
       Dim objFSO, fso
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       Set fso = objFSO.GetFolder("c:\myFolder")  'path to rename
       fso.Move ("c:\" & Range("a1"))             'rename path with name from Range  a1
    
    End Sub
    Regards,
    Antonio

  3. #3
    Registered User
    Join Date
    07-06-2007
    Posts
    70

    Re: Rename folders using values from sheet

    Hi,

    I should have said that i have around 300 rows so i would need to perform this for each row. Any ideas?

    Thanks again!

  4. #4
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Rename folders using values from sheet

    I apologize for my mistake.

    Try with this code:
    Sub Macro1()
       Dim objFSO, fso
       Dim lastRow As Long, r As Long
       Dim fromFolder As String, toFolder As String
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       
       lastRow = Cells(Rows.Count, "a").End(xlUp).Row
       
       For r = 1 To lastRow
          fromFolder = Cells(r, "a")
          toFolder = Cells(r, "b")
          If objFSO.folderExists(fromFolder) Then
             Set fso = objFSO.GetFolder(fromFolder)
             fso.Move (toFolder)
          End If
       Next r
    End Sub
    Regards,
    Antonio
    Last edited by antoka05; 04-30-2009 at 05:45 AM. Reason: solved

  5. #5
    Registered User
    Join Date
    07-06-2007
    Posts
    70

    Re: Rename folders using values from sheet

    That's exactly what i needed!!

    Thank you VERY MUCH!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1