Consulting

Results 1 to 9 of 9

Thread: VBA to create a folder and move

  1. #1

    VBA to create a folder and move

    Hi Everyone,

    Am new to this forum and i am no expert in VBA but fair in finding codes and applying it into my work.

    Am trying to achieve below objective but not successful sofar.

    1) I have file with column A, B and C

    A column = File Name
    B column = Folder Name for each file

    C1 = is the path where file to be saved.
    C2 = is the path where files are currently stored

    I need a code which will create folder(As per column B cell for each file) move the files (stored in C2 path) in it. If folder found, then just move the file and go to next file name. Do the process until last cell value in column A.

    Please help.

    Thanks

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum

    since C1 houses the destination folder why do you need the value in column B?
    is it parent folder is in C1 and check the existence of the subfolder from C1 + ColB?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    An example would help
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Quote Originally Posted by mancubus View Post
    welcome to the forum

    since C1 houses the destination folder why do you need the value in column B?
    is it parent folder is in C1 and check the existence of the subfolder from C1 + ColB?

    Yes, Parent folder is in C1 and B1 is the subfolder to be created.
    Attached Files Attached Files

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub vbax_63628_create_folders_move_files()
    
        Dim prntfldr As String, srcfldr As String, dstfldr As String
        Dim i As Long
        
        Worksheets("Sheet1").Select 'change Sheet1 to suit
        
        prntfldr = Range("C1").Value & "\"
        srcfldr = Range("C2").Value & "\"
        
        With CreateObject("Scripting.FileSystemObject")
            For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
                If Not .FolderExists(prntfldr & Range("B" & i).Value) Then .CreateFolder (prntfldr & Range("B" & i).Value)
                .MoveFile srcfldr & Range("A" & i).Value, prntfldr & Range("B" & i).Value & "\" & Range("A" & i).Value
            Next i
        End With
    
    End Sub
    ps: backup your files first or copy all in a test folder then test the code.
    pps: can be done using array(s) but this should work for you.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Hi,

    I tried with your code, am getting error "Run Time error '53: File not found
    on below line

    Sub vbax_63628_create_folders_move_files()
        Dim prntfldr As String, srcfldr As String, dstfldr As String
        Dim i As Long
        
        Worksheets("Sheet1").Select 'change Sheet1 to suit
        
        prntfldr = Range("C1").Value & "\"
        srcfldr = Range("C2").Value & "\"
        
        With CreateObject("Scripting.FileSystemObject")
            For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
                If Not .FolderExists(prntfldr & Range("B" & i).Value) Then .CreateFolder (prntfldr & Range("B" & i).Value)
                .MoveFile srcfldr & Range("A" & i).Value, prntfldr & Range("B" & i).Value & "\" & Range("A" & i).Value
            Next i
        End With
     End Sub
    Please advise

  7. #7
    Is that because of the file extension which is in .PDF?
    Last edited by shiva_reshs; 09-14-2018 at 02:02 AM.

  8. #8
     .MoveFile srcfldr & Range("A2" & i).Value & ".pdf", prntfldr & Range("B2" & i).Value & "\" & Range("A2" & i).Value & ".pdf"
    Thanks, it was indeed pdf extension.

    Working smooth now.

    Thanks a lot mancubus

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.

    yes. you should provide files' names with their extensions.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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