Code To Open Files And Tell Me The Name Of File Where Data Is Located.

  • Hi. I have a list of numbers in column A on the active worksheet. I need a code please that will open all the files within a folder on my desktop and tell me the name of the file each number in A is located. Thanks.


    Also posted here.


    https://www.mrexcel.com/forum/…e-where-data-located.html

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Any help anyone please?

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Can you please clarify what you are trying to do.


    You have a list of numbers and you want to search all files in a folder to see which number is located in which file?


    Will the required number always be in the same cell in all the files, or could it be anywhere?


    Can each number in the list be in only 1 of the files, or could it be in none or more than 1?


    How do you want the result to appear, in a message box, displayed on a sheet, if the latter what sheet and range?


    What is the name of the folder on your desktop and ,ideally, the full path to that folder.?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.



    Thanks all relevant answers above.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Is there just one sheet in each of the files? If more than 1 do all sheets need to be searched or will the number be somewhere on a particular sheet, if so what is the sheet name?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Quote from KjBox;792486

    Is there just one sheet in each of the files? If more than 1 do all sheets need to be searched or will the number be somewhere on a particular sheet, if so what is the sheet name?


    There should be only one sheet but they will all be called different names in each file, if there is more than one sheet in a file only the first will need searching if that is possible.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Working on a solution for you, will post as soon as done, but I have other commitments too. Maybe tomorrow when done.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Ok, thanks.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    I have some code ready, but it would help if you could answer the following


    On the sheet that has the list of numbers, is there a header in cell A1 and the number list starts in A2, or is there just a list starting in A1?


    With the files that are to be searched what is the maximum likely number of columns and rows of data?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Quote from KjBox;792541

    I have some code ready, but it would help if you could answer the following


    On the sheet that has the list of numbers, is there a header in cell A1 and the number list starts in A2, or is there just a list starting in A1?


    With the files that are to be searched what is the maximum likely number of columns and rows of data?


    Yes the data starts in a2 and each file could be about 10000 rows by 30 columns.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Try this. You could add a button to your sheet that has the Number List and assign the "FileSearch" macro to that button.


    All these macros should be placed in the same standard module.


    The workbook that has the Number List can be either put in the same folder as the files that have to be searched, or anywhere else. If in the same folder then it will be excluded from the files that get searched.


    The code will search the entire used range of the first sheet without needing to open each file.


    Note there are 2 places in the code where you will need to change the code to reflect the actual name of the sheet that contains the number list.



    The code will search the first worksheet of all the files in the folder (except the number list file if it is in the same folder) and record the file(s), if any, that contain each number in columns B onwards for each number in the list.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Thanks. As soon as I run it I get a runtime error '9' subscript out of range. When I debug it points to If Not IsEmpty(Files(1)) Then

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Try changing that line to

    Code
    If i > 0 Then


    If you still get the error then it could be that the path to the folder that contains the files is not the path you gave me earlier.


    Check the path to the folder and modify this line if necessary

    Code
    sPath = "C:\Users\manager\Desktop\New folder(2)\"


    Note the "" at the end, that must be included.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    If the file with the number list is kept in the same folder as the files to be searched then you can use this (the path does not need to be hard coded, so is dynamic and code will not need changing if the folder is moved or renamed).

    Code
    sPath = ThisWorkbook.Path & Application.PathSeparator


    If you use this method ensure that the number list file is saved to the correct folder and opened from there before testing the code.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Ok there was an error in the path in your code, there wasnt a space between folder and (2). The code began to run but then there was another error pointing to


    If Not IsEmpty(y(i, UBound(y, 2))) Then

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    The code ran perfectly with some test files I created.


    Can you attach your number list file and a couple of files that need to be searched. Much better to test on real situations rather than on what I think the files will be like!
    [sw]*[/sw]

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    Here is a couple of files, wouldn't know if it fails on these as there are others in the folder. I will send another next post

    Files

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    heres another

    Files

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    I tried it on those 2 files I sent you and it worked okay, got to figure out why it errored out and on which other files in the folder.


    Edit


    I tried again on all files in the folder and it seemed to work ok. Thanks for your help if i get any further problems i will let you know.


    I have noticed that it puts the same file in column B, C, D etc. next to the same number in A. Is this because it is in that file more than once?

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Always try any codes provided on a copy of your workbook. Once a macro is run it cannot be undone!


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.


    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

  • Re: Code To Open Files And Tell Me The Name Of File Where Data Is Located.


    I also ran it on the files you sent and all was OK.


    Yes that could well be the reason for the repeated file names. I did not take multiple entries of the same number in any single sheet into account as I thought it would not happen. Do you want me to amend the code to check for multiple instances of the number in a sheet and then stop the file name being repeated?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

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