Actualmente recibimos formularios electrónicos completados con Excel de nuestros clientes, por lo que tengo una carpeta llena de libros.
Cada libro tiene varias hojas.
Necesito buscar en cada uno de los libros para ver si se ha completado el rango "J8:Y8 en la hoja 3" y "G8:AC8 en la hoja 4", ya que necesitan un mayor escrutinio, pero solo unos pocos tendrán esas hojas completadas.
Además, cada libro de trabajo tiene un nombre completamente diferente.
Encontré este código (a continuación) en línea que, en principio, hace lo que necesito. Sin embargo, busca en cada hoja del libro un "Valor" específico.
Sub SearchFolders()
Dim fso As Object
Dim fld As Object
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim lRow As Long
Dim rFound As Range
Dim strFirstAddress As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
'Change as desired
strPath = "c:\MyFolder"
strSearch = "Specific text"
Set wOut = Worksheets.Add
lRow = 1
With wOut
.Cells(lRow, 1) = "Workbook"
.Cells(lRow, 2) = "Worksheet"
.Cells(lRow, 3) = "Cell"
.Cells(lRow, 4) = "Text in Cell"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)
strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open _
(Filename:=strPath & "\" & strFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)
For Each wks In wbk.Worksheets
Set rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then
lRow = lRow + 1
.Cells(lRow, 1) = wbk.Name
.Cells(lRow, 2) = wks.Name
.Cells(lRow, 3) = rFound.Address
.Cells(lRow, 4) = rFound.Value
Else
Exit Do
End If
Set rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next
wbk.Close (False)
strFile = Dir
Loop
.Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"
ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Respuesta1
Este código funcionará de la siguiente manera:
- Abra un nuevo libro de trabajo donde quiera.
- Pegue el código VBA en Macro
En la celda A1 de la Hoja 1, coloque la ruta a la carpeta de libros, por ejemplo:
C:\users\yourname\folder\
En la celda A2, el primer rango, por ejemplo:
J8:Y8
y en la celda B2, el nombre de la hoja:Sheet3
- En la Celda A3 el segundo rango por ejemplo:
G8:AC8
y en la Celda B3 el nombre de la Hoja:Sheet4
Lo mejor de este código es que si tiene más rangos/hojas para buscar, puede agregar en las siguientes filas.
Se verá así:
Ahora, ejecute la macro y después de su ejecución mostrará los resultados enHoja2, mostrando el nombre del archivo y el número de celdas vacías en cada rango.
Sub foldersearch()
Dim wbk As Workbook
Dim wbk1 As Workbook
Dim wks As Worksheet
Dim wks2 As Worksheet
Dim totaltime As Long
Dim dtDuration As Date
Set wbk = ThisWorkbook
Set wks = wbk.Sheets(1)
Set wks2 = wbk.Sheets(2)
starttime = Now()
wks2.Cells.ClearContents
dirPath = wks.Cells(1, 1)
file = Dir(dirPath)
rowscounter = 0
Application.ScreenUpdating = False
While (file <> "")
If InStr(file, "xls") > 0 Then
rowscounter = rowscounter + 1
totalpath = dirPath & file
Set wbk1 = Workbooks.Open(totalpath, , True)
rangelist = True
i = 2
columnscounter = 2
While rangelist = True
thenewrango = wks.Cells(i, 1)
thenewsheet = wks.Cells(i, 2)
emptycount = workbooksearch(wbk1, thenewsheet, thenewrango)
wks2.Cells(rowscounter, 1) = file
wks2.Cells(rowscounter, columnscounter) = emptycount
i = i + 1
columnscounter = columnscounter + 1
If wks.Cells(i, 1) = "" Then
rangelist = False
End If
Wend
wbk1.Close (False)
End If
file = Dir
Wend
Application.ScreenUpdating = True
endtime = Now()
totaltime = DateDiff("s", starttime, endtime)
a = MsgBox("Finished in" & vbCrLf & totaltime & " seconds", vbOKOnly)
End Sub
Function workbooksearch(wbk1 As Workbook, wksname As Variant, rango As Variant)
Dim wks1 As Worksheet
Dim obj As Object
On Error GoTo HandleError
Set obj = wbk1.Sheets(wksname)
Set wks1 = wbk1.Worksheets(wksname)
emptycount = 0
For Each c In wks1.Range(rango)
If c.Value = "" Then
emptycount = emptycount + 1
End If
Next c
workbooksearch = emptycount
Exit Function
HandleError:
workbooksearch = "N/A"
End Function