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?
thanks in advance.Please Login or Register to view this content.
max_max
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?
thanks in advance.Please Login or Register to view this content.
max_max
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
An aid for editing?
maybe this will help
http://mikevba.altervista.org/macro/macro012.htm
Editing for research even in sub folders is not possible?
max_max
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.
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!)
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.
Hello to all,
maybe I explained myself wrong.
max_max
Hello max_max,
By "research" did you mean to search recuirsively?
La parola "research" si intende una ricerca in modo ricorsivo?
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
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
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.
@ 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.
Hello max_max,
Since I cannot change my post #12, I will try to repost the module code again.
Module1 Code
Module2 CodePlease Login or Register to view this content.
Please Login or Register to view this content.
Last edited by Leith Ross; 02-24-2017 at 01:34 AM.
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
This is the original file with your macro.
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
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.
Hello Leith Ross
Now it works fine.
Thanks so much.
max_max
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
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?
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
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
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
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.
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
Last edited by max_max; 02-28-2017 at 03:08 PM.
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks