Results 1 to 3 of 3

Thread: Copy/Paste files into new folders based on condition

  1. #1
    Seeker technik's Avatar
    Join Date
    Oct 2018
    Posts
    10
    Articles
    0
    Excel Version
    MS 2016

    Copy/Paste files into new folders based on condition



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Hi

    I am trying to create a macro wherein 20 folders are created (names of folders are from the range of cells e.g. B2:B21). This seems to work OK.

    I am then trying to copy 5 (different) pdf files (which are chosen according to a criteria ie. score of <=50%) into each of the 20 folders. This bit is not working properly and I think it's because the cell.Value (which contains the pdf location) is just a file path so the actual physical pdf is not being copied? Please see code below.

    Can anyone help me with copying the relevant pdf files across into each of the 20 folders. Or alternative could be that all 5x20=100 pdfs are coped into 1 folder but sorted in some way so I know which pdfs belong to which candidate?

    I've attached my files also. Would really appreciate any help here as I'm not sure I am doing this correctly.

    Also I've commented out the shell call which I know you can use to print ... but I need copy/paste into new folders instead.


    'CREATE INDIVIDUAL FOLDERS
    On Error Resume Next

    For Each cell In rng2
    FldrName = cell
    MkDir "C:\Users\test\Documents\Files" & FldrName
    Next cell


    'COPY PDF FILES IN COLUMN D TO CREATED FOLDERS
    For Each cell In rng1
    If cell.Offset(0, 3) <= 0.5 And count <= 5 Then
    If Not dnary.Exists(cell.Value) Then
    dnary.Add cell.Value, 1
    FileCopy cell.Value, "C:\Users\test\Documents\Files" & FldrName
    'Call apiShellExecute(Application.hwnd, "print", cell.Value, vbNullString, vbNullString, 0)count = count + 1
    End If
    End If
    Next cell
    Attached Files Attached Files

  2. #2
    Neophyte Richard Wein's Avatar
    Join Date
    Oct 2017
    Location
    UK
    Posts
    4
    Articles
    0
    Excel Version
    365
    Hi technik. I haven't looked at the details, but I have a general suggestion to make. Delete the "On Error Resume Next" statement. It's making it harder for you to find the problem. If an error occurrs, you want VBA to stop, report the error, and give you the opportunity to enter Debug mode. On Error Resume Next is telling VBA to carry on regardless of any errors, so it doesn't tell you about the errors it detects.

    Many beginners think they should put "On Error Resume Next" at the start of every.macro. This is a mistake. It should only be used in certain specific circumstances.

  3. #3
    Seeker technik's Avatar
    Join Date
    Oct 2018
    Posts
    10
    Articles
    0
    Excel Version
    MS 2016
    Thanks for that Richard - will try it.

Posting Permissions

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