Copy as Path

  • I need help with a coding requirement that I've not previously experienced. I have a number of folders that each contain about 100 small .csv files; for each folder I need to copy the path for each file to an open worksheet. Each folder of .csv files has its own associated workbook.


    As one example, the open workbook is F:\SM\M400AD.xlsm and the active worksheet is CSV_List. The folder containing the .csv files is F:\SM\M400AD
    Doing it manually, my sequence is then:
    Open folder F:\SM\M400AD
    Select all
    Copy as path
    Paste to Range("B11") of worksheet CSV_List


    I need to automate this and would be grateful if a VBA guru could kindly code this for me.

  • Re: Copy as Path


    Hi pike


    Yes, you're partially correct; the workbook M400AD.xlsm is in folder F:\SM\ and the csv files are in the folder F:\SM\M400AD\


    But no, I don't want a list of the file names in folder F:\SM\M400AD\; I need a list of the paths. This where I'm lost - even more lost than usual! I've never previously used VBA to copy paths. When I do it manually, as described above, I get a list that looks like:
    "F:\SM\M400AD\AC1.csv"
    "F:\SM\M400AD\AC2.csv"
    "F:\SM\M400AD\AE.csv"
    "F:\SM\M400AD\AF.csv"
    "F:\SM\M400AD\AG.csv"
    "F:\SM\M400AD\AH1.csv"
    "F:\SM\M400AD\AH2.csv"
    "F:\SM\M400AD\AJ.csv"
    and on down the page until I have a list of 100 paths. This single column list is then pasted into worksheet CSV_List, starting at Range("B11"). Just FYI, once I have that list of paths pasted into CSV_List I then import the csv files into the workbook - hence the need for paths, not just file names.

  • Re: Copy as Path


    thought so ..

  • Re: Copy as Path


    Thanks pike - but you're losing me slightly. At which point do I give the coding the detail of my folder that contains the csv files? Will it give me output that can then be copied into CSV_List?

  • Re: Copy as Path


    No matter where I run the code from I get a Run-time error '76' pop-up - Path not found.


    Debug highlights the line:
    Set objFolder = objFSO.GetFolder(ThisWorkbook.Path & "" & Split(ThisWorkbook.Name, ".")(0))


    I figured from that that I need to input the folder path detail somewhere

  • Re: Copy as Path


    You may try something like this.....

    Regards.
    sktneer

  • Re: Copy as Path


    Hi pike


    When I'm trialing code I initialise it from the code module. Once it's working I assign it to a button on the relevant worksheet.


    I've no idea what a folder picker is, therefore have no idea whether I need one - or not.

  • Re: Copy as Path


    run this code to pick a folder to copy the full path of the files within that folder

  • Re: Copy as Path


    pike / sktneer_1
    Sorry to be so long responding. I'd been at it yesterday for close on 20hr and it was doing my head in! I'm very much a novice programmer and what's simple to you guys is bewilderment to me.


    Anyway - the outcome is all I could have wished for. I've tested both your last posted coding using test workbooks and data and the results are perfect.


    Thank you both so much for your assistance with this query and pike, thank you for your patience and help with this and my various previous queries.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!