+ Reply to Thread
Results 1 to 28 of 28

macro to folders 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 to folders and subfolders

    Hello to all.
    The macro attached to search the files in the folder.
    it is possible that the macro looks for even in sub-folders?


    Please Login or Register  to view this content.
    thanks in advance.
    max_max

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: macro to folders and subfolders

    Google: excel vba loop through all files folder subfolders

    You'll get lots of examples.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  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 to folders and subfolders

    An aid for editing?

  4. #4
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: macro to folders and subfolders


  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 to folders and subfolders

    Editing for research even in sub folders is not possible?
    max_max

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: macro to folders and subfolders

    I'm not sure what you are asking for ... I don't understand the Italian so I can't adjust your code.

    The Google search I suggested will help you to loop through all the files in folders and their sub-folders. What you do in terms of processing the files is entirely up to you but, realistically, it's what you are currently doing for each of the files in the top level folder.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Ciao max_max,

    Come va? Si prega di spiegare in italiano "Editing for research".
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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

    Re: macro to folders and subfolders

    Hello I hope to explain.
    The macro attached to research in the folder:

    -------------------------------------------------------------------------------------------
    Comm1 = ActiveSheet.Range("B1").Value '<<< nome cella 1
    Comm2 = ActiveSheet.Range("B2").Value '<<< nome cella 2
    Comm3 = Comm1 & "-" & Comm2 '<<< unite

    .LookIn = "C:\Users\massimo\Desktop\MAX\moduli_salvati\" & Comm3 & "\"
    -------------------------------------------------------------------------------------------

    file *.xls
    If in this folder are sub folders
    the macro does not find files that are within these subfolders.
    You can edit the macro because you can find the files * .xls that they are inside in subfolders?.
    I hope I explained
    max_max

    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 to folders and subfolders

    Hello to all,
    maybe I explained myself wrong.
    max_max

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Hello max_max,

    By "research" did you mean to search recuirsively?
    La parola "research" si intende una ricerca in modo ricorsivo?

  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 to folders and subfolders

    I hope to explain myself with google translator:
    search_file.xls as it is set to search the * .xls file in the folder:

    ---------------------------------------------------------------------------
    Comm1 = ActiveSheet.Range("B1").Value '<<< nome cella 1
    Comm2 = ActiveSheet.Range("B2").Value '<<< nome cella 2
    Comm3 = Comm1 & "-" & Comm2 '<<< unite

    .LookIn = "C:\Users\massimo\Desktop\MAX\" & Comm3 & "\"
    ----------------------------------------------------------------------------

    but if in this folder there are subfolders within other files * .xls search_file.xls not find them.
    The modification that I ask, it is always possible, is that search_file.xls are all * .xls.
    I hope I was clear.
    max_max

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Hello Max,

    I have added a new VBA module to original code. This module searches subfolders recursively to any depth.

    Here is the code that has been added to the attached workbook.

    Module1 Code
    Copy to clipboard
    'Option Explicit

    Public ApplicationFileSearch As New FileSearch


    '=================================================
    'non cambiare il nome a questo file
    '=================================================


    Sub apri_e_verifica()
    Dim obiettivo
    Dim A As Range
    Dim trova As FileSearch
    Dim cartella As Integer
    Dim x As Long
    Dim cl As Range
    Dim nome_file As String
    Dim FoundFiles() As Variant

    Dim Comm1, Comm2, Comm3 As String

    Dim vuoto As Boolean
    Set trova = ApplicationFileSearch
    vuoto = False


    'IMPOSTO LA VARIABILE "x" COME INDICE DI RICERCA DELL'ULTIMA RIGA
    'OCCUPATA DELLA COLONNA A DEL FILE "search_file"

    x = Workbooks("search_file.xls").Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1

    'CANCELLO IL RANGE DOVE VERRANNO INSERITI I DATI

    Workbooks("search_file.xls").Sheets(1).Range("A4:C" & x).ClearContents

    obiettivo = InputBox("Inserisci il codice da cercare", "Ricerca codice")
    Application.ScreenUpdating = False

    'SE L'INPUTBOX è VUOTA, AVVISA ED ESCE DALLA ROUTINE

    If obiettivo = "" Then
    vuoto = True
    MsgBox "devi inserire un codice", vbExclamation, "ATTENZIONE"
    End If
    If Not vuoto Then

    'With trova

    'INDICO IL PERCORSO DELLA CARTELLA DOVE SONO I FILE DA APRIRE

    Comm1 = ActiveSheet.Range("B1").Value '<<< nome cella 1
    Comm2 = ActiveSheet.Range("B2").Value '<<< nome cella 2
    Comm3 = Comm1 & "-" & Comm2 '<<< unite

    ' Search All SubFolders in the Parent folder's SubFolders and their SubFolders, etc.
    Call GetSubFolders("C:\Users\massimo\Desktop\MAX\moduli_salvati\" & Comm3 & "\", FoundFiles, -1)

    '.LookIn = "C:\Users\massimo\Desktop\MAX\moduli_salvati\"

    'INDICO IL TIPO DI FILE DA APRIRE

    '.filename = "*.xls"

    'METODO EXECUTE RESTITUISCE IL NUMERO DEI FILES DELLA
    'DIRECTORY SPECIFICATA; SE è = 0 AVVISA ED ESCE DALLA ROUTINE

    If UBound(FoundFiles) >= 0 Then

    'CICLO FOR CHE APRE TUTTI I FILE CONTENUTI NELLA CARTELLA "moduli_salvati"
    'CON LA PROPRIETà "FoundFiles"
    'DALL'HELP ON LINE :
    'Restituisce un oggetto FoundFiles contenente i nomi di tutti i
    'file trovati durante la ricerca. Di sola lettura.
    ' E CON LA PROPRIETà "Count" CHE RESTITUISCE IL NUMERO
    'DELL'ULTIMO FILE

    For cartella = 0 To UBound(FoundFiles) - 1
    Workbooks.Open FoundFiles(cartella)
    nome_file = ActiveWorkbook.Name
    Set A = Workbooks(nome_file).Sheets(1).Range("L7:L31")

    'CICLO PER VERIFICARE L'ESISTENZA DEL CODICE NEI FILES; SE LO TROVA,
    'INSERISCE IL CODICE, IL NOME DEL FILE E L'INDIRIZZO DELLA CELLA
    'NEL FILE "search_file", ED ESSENDO UN CODICE UNIVOCO, ESCE DAL CICLO E
    'PASSA AL FILE SUCCESSIVO

    For Each cl In A
    If cl = obiettivo Then
    With Workbooks("search_file.xls").Sheets(1)

    'RE-IMPOSTO LA VARIABILE "x" COME INDICE DI RICERCA DELL'ULTIMA RIGA
    'OCCUPATA DELLA COLONNA A DEL FILE "search_file"

    x = .Cells(Rows.Count, 1).End(xlUp).Row + 1
    .Range("A" & x) = cl
    .Range("B" & x) = Workbooks(nome_file).Name
    .Range("C" & x) = cl.Address(False, False)
    End With
    Exit For
    End If
    Next cl

    'CHIUDO LA CARTELLA ATTIVA

    Workbooks(nome_file).Close

    'PROSSIMO FILE DA APRIRE

    Next cartella

    Else
    MsgBox "Nella directory scelta non ci sono Files"
    End If
    End With

    If Workbooks("search_file.xls").Sheets(1).Range("A4") = "" Then
    MsgBox "Non è stato trovato nessun codice " & obiettivo, vbExclamation, _
    "VERIFICA CONCLUSA"
    Else
    MsgBox "Il codice " & obiettivo & " è stato trovato in " & x - 3 & " file/s", _
    vbInformation, "VERIFICA CONCLUSA"
    End If
    End If
    Application.ScreenUpdating = True
    Set A = Nothing
    Set trova = Nothing
    End Sub
    Module2 Code
    Copy to clipboard
    Global oShell As Object

    Sub GetSubFolders(ByVal Folder As Variant, ByRef FilePathList As Variant, Optional ByVal SubFolderDepth As Long)

    Dim n As Variant
    Dim oFiles As Object
    Dim oFolder As Object
    Dim SubFolder As Variant
    Dim SubFolders As Object

    If VarType(FilePathList) = vbArray + vbVariant Then
    On Error Resume Next
    If Err <> 0 Then ReDim Preserve FilePathList(0)
    On Error GoTo 0
    End If

    If oShell Is Nothing Then
    Set oShell = CreateObject("Shell.Application")
    End If

    Set oFolder = oShell.Namespace(Folder)

    If oFolder Is Nothing Then Exit Sub

    Set oFiles = oFolder.Items
    oFiles.Filter 64, "*.xls"

    For n = 0 To oFiles.Count - 1
    x = oFiles.Item(n).Path
    FilePathList(UBound(FilePathList)) = oFiles.Item(n).Path
    ReDim Preserve FilePathList(UBound(FilePathList) + 1)
    Next n

    Set SubFolders = oFolder.Items
    SubFolders.Filter 32, "*"

    For Each SubFolder In SubFolders
    Attached Files Attached Files

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Okay, over the years I have seen some strange things happen on this forum. But my last post takes it. I can not even edit the system errors in the post at this time.

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: macro to folders and subfolders

    @ Leith

    Just wanted to report your post for using code tags.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Hello max_max,

    Since I cannot change my post #12, I will try to repost the module code again.

    Module1 Code
    Please Login or Register  to view this content.
    Module2 Code
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 02-24-2017 at 01:34 AM.

  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 to folders and subfolders

    Hello leithross,
    nothing works the macro crashes.

    --------------------------------------------------------------------------
    Public ApplicationFileSearch As New FileSearch '<<<<<<<<<<<<<

    compilation error:
    Invalid Use the New keyword
    --------------------------------------------------------------------------

    (Error translated with google translator)

    max_max

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

    Re: macro to folders and subfolders

    This is the original file with your macro.
    Attached Files Attached Files

  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 to folders and subfolders

    He lacked a class module in vba.
    Now a new error:

    Run time 9 Error
    index out of range

    here:
    If UBound (FoundFiles)> = 0 Then <<<< (module1)

    I am attaching the new file.
    max_max
    Attached Files Attached Files

  19. #19
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Hello Max,

    The workbook has been tested and works correctly as is. Please do not add any additional code or change any code in this workbook.

    Let me if have any issues so I can address them accordingly.

    You can download it from here...

    Search_File ver 1.xls
    Last edited by Leith Ross; 02-24-2017 at 11:26 PM.

  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 to folders and subfolders

    Hello Leith Ross
    Now it works fine.
    Thanks so much.
    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 to folders and subfolders

    Hello leith ross.
    There is an error on the count of the files found.
    In search_file displays found the exact file but in the InputBox "VERIFICA CONCLUSA" the count is different.
    max_max

  22. #22
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Hello Max,

    I have run the macro several times. Each time the number of rows of file information on the worksheet matches the number of files in the message box "VERIFICA CONCLUSA".

    Something is different, Can you post a few examples of this problem?

  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 to folders and subfolders

    Hello leith ross.
    I made a small change to your last version, I added in the upper path to search.
    I attach the new version and an example of the research I did as evidence.
    The example test I plugged in a folder and subfolders on the desktop for 15 times.
    The macro search_file finds all the 15 copies of evidence but in the imputbox are 23 files.
    Perhaps the fault of rows added at the top?
    I hope what I wrote is understandable
    max_max
    Attached Files Attached Files

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

    Re: macro to folders and subfolders

    Hello leith ross.
    I think my mistake:

    MsgBox "Il codice " & Chr(34) & obiettivo & Chr(34) & " è stato trovato in " & UBound(FoundFiles) & " file/s", vbInformation, "VERIFICA CONCLUSA" '<<< ok
    'MsgBox "Il codice " & Chr(34) & obiettivo & Chr(34) & " è stato trovato in " & x - 1 & " file/s", vbInformation, "VERIFICA CONCLUSA"

    max_max

  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 to folders and subfolders

    I did other tests, and I think it's right, you're strong leith ross.
    It can a change, it is always possible, adding the path of files as in attached picture?
    max_max
    Attached Images Attached Images

  26. #26
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: macro to folders and subfolders

    Hello Max,

    I have had a lot of difficulty the past few days in posting and answering private messages. The problem is most likely one of the servers in the chain to the Excel Forum.

    Both macros needed to be updated to handle the new column "B" for the folder path.

    Here is the updated code. I will try to post the workbook also.

    Module1 Code
    Please Login or Register  to view this content.

    Module2 Code
    Please Login or Register  to view this content.

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

    Re: macro to folders and subfolders

    Hello leith ross.
    I updated the modules as written in the previous post, but it does not work, if I done correctly.
    I attaching photos of the error and the modified file, also I attach for searching.

    VBA > F8 error here:

    Workbooks.Open FoundFiles(cartella, 1) & "\" & FoundFiles(cartella, 2) '<<<
    If Err.Number = 9 Then '<<<
    MsgBox "The path """ & Path & """ is missing or the name has been changed.", vbCritical, "Path Error" '<<<
    Exit Sub '<<<

    max_max
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by max_max; 02-28-2017 at 03:08 PM.

  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 to folders and subfolders

    Hello the file does not work perfectly:
    After clicking check > insert item (inserisci articolo)
    you view

    save changes? cancel >

    I click cancel and then find the requested article.
    Let's fix this?
    max_max
    Attached Files Attached Files

+ 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. [SOLVED] Macro to Copy and paste folders and subfolders without files in it
    By Madhut in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2016, 01:47 PM
  2. Create folders in all the subfolders and move subfolders
    By Amarjeet Singh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2015, 12:51 PM
  3. Needs macro to copy excel files from folders, subfolders to new folder
    By genetist in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-08-2014, 08:53 AM
  4. Macro to list a specific type of file searching in folders and subfolders
    By vamu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-15-2014, 09:31 AM
  5. Macro Code to Encrypt Multiple Excel Files in Folders and Subfolders
    By ali_1989 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-04-2014, 12:53 PM
  6. Macro to list folders and contents within it Including subfolders.
    By pekunda in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-12-2012, 08:17 AM
  7. Dir folders and subfolders macro writes over it self
    By Pero in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2009, 02:46 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