+ Reply to Thread
Results 1 to 30 of 30

macro search in folder and subfolders

  1. #1
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    macro search in folder and subfolders

    Hello to all,
    you can change the macro attached because you can find your files in folder and subfolders ?:

    Please Login or Register  to view this content.
    max_max

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    You need to add a little bit of recursion, like this - which means some of your code that should only be run once needs to be moved to a separate macro, some of your variables need to be made public, and your original macro needs to be passed the path as an argument. You may also want to store the path as well as the filename: if you do, change

    .Cells(lRow, 1) = wbk.Name

    to

    .Cells(lRow, 1) = wbk.FullName

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Error here:

    Sub SearchFolders(strPath As String)
    Dim fso As Scripting.FileSystemObject


    compilation error
    defined type not defined

    max_max

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    Sorry - You need to set a reference to MS Scripting Runtime.

    Capture.JPG

  5. #5
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    O.k. thanks bernie.
    Sorry for my English I translate with google translator
    you can change this?:

    1) display folders and subfolders in sheet2
    2) does not display the msgbox processing ....
    but only the final result. > done
    3) for new search reset all sheet2

    I hope I explained.
    max_max

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    Try this - not sure why you would reset all sheet2 since the code creates a new sheet every time the code is run.


    Please Login or Register  to view this content.

  7. #7
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    O.k. thanks bernie.
    Sorry for my English I translate with google translator
    you can change this?:

    1) display folders and subfolders in sheet2 <<< O.k. done
    2) does not display the msgbox processing ....but only the final result. > done <<< not done modified
    3) for new search reset all sheet2 <<< not done modified

    I hope I explained.
    max_max

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    Maybe this for #3 - see changes in bold. Not sure what you mean by #2 or what you want instead.

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello bernie,
    I did not see your last post and in the meantime I changed the macro by adding the msgbox but now does not work.
    max_max
    Attached Files Attached Files

  10. #10
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello bernie,
    Now works almost everything, vba does not work in the sheet name "ricerca"
    max_max
    Attached Files Attached Files

  11. #11
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    I was able to correct the name of Sheet2, remain one corrections:
    msgbox "done/fatto" must appear only one time at the end of the research
    I hope I explained.
    max_max
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    Move your message box from SearchFolders to startSearch:

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello bernie is perfect!
    Thanks so much!
    Another thing is possible That the workbook found become a link to view the file immediately?
    Many thanks again.
    max_max
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello bernie,
    I tried the new macro here at home and work.
    Tomorrow morning I try the macro in my office.
    Thank you so much.
    A greeting and thanks again.
    max_max

  16. #16
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello bernie the macro works well!
    If it is not too complicated, you can change this ?:
    Now to find a folder I have to write in the folder D1
    it is possible in addition to this method with a double click in D1
    that you open all the subfolders that are in the path to B1?
    I hope I explained.
    A greeting and thanks again.
    max_max

  17. #17
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    You could need to use a macro to open all the folders in the tree (which is probably a bad idea anyway). Once you open the root folder, then you can navigate to any subfolder of interest.

    If you wanted, you could open the folder named in the cell in column D by using the before double-clcik event code:

    1) Copy this code.
    2) Right-Click the sheet tab of interest.
    3) Select "View Code"
    4) Paste the code into the window that appears.
    5) Save the file as a macro-enabled .xlsm file.
    6) Make changes as needed

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello bernie,
    great.
    it is possible instead for the whole column D only one of the D column cell for example D2?
    Thank you,
    max_max

  19. #19
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    It will open the folder in the cell in D that you double-click on - so, if you want D2, double-click D2.

  20. #20
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders


    Thanks Bernie.
    max_max

  21. #21
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello,
    is a change to the macro possible?
    Everything works well, just a small problem.
    The macro must also work on merged cells.
    If the search is on workbook with merged cells this error.

    Runtime Error -2147352565 (8002000b)
    impossible to find the searched data

    Thanks in advance.
    max_max

  22. #22
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    Which line in which macro produces the error?

  23. #23
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    gest_err:

    path2 = Worksheets("search").Range("B2").Text '<<< path

    If Err.Number = 76 Then

    MsgBox "The path """ & path2 & """ " & Chr(13) & "is missing or the name has been changed.", vbCritical, "PATH ERROR"
    'MsgBox "Il path """ & Path2 & """ " & Chr(13) & "non esiste o è stato cambiato.", vbCritical, "PATH ERRORE"

    Exit Sub
    End If


    End Sub


    VBA > F8 = image1

    workbook > macro = image2
    Attached Images Attached Images

  24. #24
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: macro search in folder and subfolders

    There is nothing wrong with that line of code - just make sure that the workbook that is active at the time has the sheet "Search" since that defaults to the activeworkbook. If you are opening some other workbook in other code, you may need to change it to include the workbook object, like

    Please Login or Register  to view this content.

  25. #25
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hi Bernie,
    everything works fine if there are no greasy cells
    The only problem is only if the word / number to be searched is done on a workbook with merged cells.

    Another information.
    Here in:
    Sub SearchFolders (strPath As String)
    the search is only with xls

    With wOut
    strFile = Dir(strPath & "\*.xls*")
    Do While strFile <> ""

    the search is on xls/ xlsx / xlsm or only xls?
    max
    Last edited by max_max; 04-08-2018 at 07:33 AM.

  26. #26
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hi,
    I partially corrected the macro.
    Now it works even with merged cells
    Hi added in:

    -----------------------------------------------------------------
    Sub SearchFolders(strPath As String)
    Dim fso As Scripting.FileSystemObject
    Dim fld As Scripting.Folder
    Dim sFolder As Scripting.Folder
    Dim strFile As String
    Dim rFound As Range
    Dim strFirstAddress As String
    'Dim valori As String

    On Error Resume Next
    "
    "
    "
    "
    "
    On Error GoTo 0

    End Sub
    -----------------------------------------------------------------


    Now this does not work:


    -----------------------------------------------------------------
    Sub startSearch()
    "
    "
    "
    "

    gest_err:

    path2 = Worksheets("search").Range("B2").Text '<<< path

    If Err.Number = 76 Then

    'MsgBox "The path """ & path2 & """ " & Chr(13) & "is missing or the name has been changed.", vbCritical, "PATH ERROR"
    MsgBox "Il path """ & path2 & """ " & Chr(13) & "non esiste o è stato cambiato.", vbCritical, "PATH ERROR/PATH ERRORE"


    Exit Sub
    End If

    End Sub
    -----------------------------------------------------------------

    the complete macros
    Please Login or Register  to view this content.
    max

  27. #27
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: macro search in folder and subfolders

    max,

    Please update your posts to include code tags.
    HTH
    Regards, Jeff

  28. #28
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello sorry for my english but my last post seems to me okay

  29. #29
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    hello to all, maybe I explained myself badly?
    max_max

  30. #30
    Forum Contributor max_max's Avatar
    Join Date
    06-28-2013
    Location
    italy - venice
    MS-Off Ver
    Excel 2007
    Posts
    1,693

    Re: macro search in folder and subfolders

    Hello, now the macro searches a text in B2.
    you can change to search file extensions for sample: xls - xlsx - pdf - xlsm etc ... non text


    Please Login or Register  to view this content.
    max_max

+ 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. Search through folder and subfolders a pdf file
    By JPGraphX in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2022, 06:11 AM
  2. Replies: 1
    Last Post: 10-19-2016, 04:44 PM
  3. Replies: 0
    Last Post: 01-16-2015, 11:39 AM
  4. Search for an excel workbook beginning with... in a folder and subfolders
    By redmarko in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-27-2013, 02:21 AM
  5. how to search a file in folder and subfolders
    By rakeshredround in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-05-2012, 03:35 AM
  6. Macro to search folder including subfolders for file and open
    By kiraexiled in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 02:45 PM
  7. Replies: 2
    Last Post: 03-26-2012, 07:12 PM

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