+ Reply to Thread
Results 1 to 15 of 15

VBA code to automatically move files to subfolders based on a predefined list

  1. #1
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    VBA code to automatically move files to subfolders based on a predefined list

    VBA code to automatically move files to subfolders based on a predefined list

    I have a folder containing over 1000 files. I want to move these files to subfolders based on a predefined list in Excel that corresponds filenames to the name of the subfolders to which the file should be moved to, such as the example below:

    table.png

    Could anyone please develop a VBA code to do this?
    Thanks so very much in advance.
    Attached Images Attached Images

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Do all subfolders exist or does this need to be checked ?
    Does Column A & Column B contain fullpath or only folder names ?
    Does Colmumn A also has the file extensions in it ?
    To give you an idea.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Thanks Bakerman for your response.
    -Subfolders do not exist. The code should also create the subfolders within the main folder.
    -Columns A & B both contain only file and folder names and not the fullpath
    -Column A does not have the file extension in it.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: VBA code to automatically move files to subfolders based on a predefined list

    To summarize.
    1) You have a starting folder holding all files f.i. "C:\Startfolder\"
    2) Column A has all filenames without extension.
    3) Column B has all required subfoldernames.

    Your need
    1) Find file in startfolder based on partial name in column A
    2) Check if subfolder in Column B exists. If Yes move file to subfolder.
    If Not create subfolder and move file.
    3) Continue till list is finished in Column A.

  5. #5
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA code to automatically move files to subfolders based on a predefined list

    All correct

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: VBA code to automatically move files to subfolders based on a predefined list

    OK then try this.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Thank you so much.I do not see the reply I had posted earlier, so here it is again:

    "I made the necessary changes to suit, but running the code I get [Run time error 53: File not found] error."

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Most likely you have a typo in 1 of the values in Column A because my tests showed no errors at all.

  9. #9
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Double checked it, the names match properly. I just can't figure our where it stems from.
    I do not get the point about the annotation in line 15: [' move the file]. I do not change anything in this line. Could the error have something to do with this?

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: VBA code to automatically move files to subfolders based on a predefined list

    No, it's just to point out that this line actually does the moving.

    Post your code as you rae using it now.

  11. #11
    Registered User
    Join Date
    03-06-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    23

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Sub tst()
    Dim fname As String
    sn = Sheets("Sheet1").Cells(1).CurrentRegion.Value 'change Sheet1 to suit
    Const StartFolder As String = "C:\subfolder_test\" 'change to correct startfolder
    With CreateObject("scripting.dictionary")
    For i = 2 To UBound(sn)
    x0 = .Item(sn(i, 2))
    Next
    For Each it In .keys
    CreateObject("shell.application").Namespace(StartFolder).NewFolder it
    Next
    End With
    For i = 2 To UBound(sn)
    fname = sn(i, 1): fname_ext = GetFullFileName(StartFolder, fname)
    Name StartFolder & fname_ext As StartFolder & sn(i, 2) & "\" & fname_ext ' move the file
    Next
    End Sub

    Function GetFullFileName(strfilepath As String, strFileNamePartial As String) As String
    Dim objFile As Variant, strfilenamefull As String
    With CreateObject("Scripting.FileSystemObject").GetFolder(strfilepath)
    For Each objFile In .Files
    If InStr(objFile.Name, strFileNamePartial) > 0 Then strfilenamefull = objFile.Name: Exit For
    Next objFile
    End With
    GetFullFileName = strfilenamefull
    End Function

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Just tested again and the only time I can raise an error is when I put a wrong filename in column A.
    When the error rises go to debug and hoover with your mouse pointer over sn(i,1) to see which value it has. Then you know which filename throws the error.

  13. #13
    Registered User
    Join Date
    11-28-2021
    Location
    Israel
    MS-Off Ver
    365
    Posts
    1

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Hi,

    Would like to revive this.
    I have the same work to do with many files in one directory.

    I am trying to run the VBA code from this tread but receive an error =

    Run-time error '91'
    Object variable or With block variable not set

    -----

    I am using latest version of excel 365 business edition.

    Thanks

  14. #14
    Registered User
    Join Date
    06-11-2022
    Location
    new york
    MS-Off Ver
    Microsoft Office 2019
    Posts
    6

    Re: VBA code to automatically move files to subfolders based on a predefined list

    thanks you solved my problem

  15. #15
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: VBA code to automatically move files to subfolders based on a predefined list

    Possibly...
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] list of subfolders in folder - without files and sub-subfolders
    By MartyZ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2022, 10:56 AM
  2. Move huge files to dynamic subfolders
    By gaurde in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-27-2015, 03:44 PM
  3. [SOLVED] Open the predefined files automatically
    By thilag in forum Excel General
    Replies: 2
    Last Post: 10-16-2013, 01:54 AM
  4. move files to folder based on a .igy list
    By henkdevreisch in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-14-2013, 01:42 PM
  5. Replies: 3
    Last Post: 10-27-2008, 08:32 AM
  6. [SOLVED] Map/List of folders, subfolders & files
    By Bogdan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2006, 01:10 PM
  7. [SOLVED] copy subfolders, replace text in files and save files in copied subfolders
    By pieros in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-01-2005, 09:05 AM

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