En esta línea tienes un par de problemas:
Workbooks(libro). Worksheets(hoja).Cells(fila, j) = Workbooks(Filename). Worksheets(hoja). Cells(i, j)
Al inicio de la macro copia(filename) estás declarando la variable fila:
Dim fila As Integer
Entonces fila es igual a 0, por consiguiente, cuando utilizas .cells(fila, j) estás tratando de poner un dato en la fila 0, esa fila no existe, entonces te envía un error.
Para solucionar este detalle tienes que poner el valor de fila después de la hoja, por ejemplo, si vas a escribir después de la última fila, podría ser así:
Fila = Workbooks(Libro). Sheets(Hoja).Range("A" & Rows. Count).End(xlUp). Row + 1
Otro detalle es, si en el libro que abriste no tiene una hoja llamada "B-Ends", también te va a enviar un error. Podrías resolverlo, revisando cada hoja del libro abierto, si tiene una hoja con ese nombre, entonces copiar los datos, si no tiene una hoja con ese nombre, entonces cerrarlo.
Bien, otro problema en tu macro, tienes 3 ciclos for anidados, el primero va de 9 a 1000, 991 veces, el segundo va de 1 a 70, entonces 991 * 70 = 69,370 veces, el último va de 1 a 176, entonces 69,370 * 176 = 12,209,120 (doce millones de veces), eso significa que tu ciclo se repite 12 millones de veces. Lo voy a cambiar a copiar y pegar.
Te anexo el código actualizado, prueba y me comentas
Sub abrir()
Application.ScreenUpdating = False
Ruta = "C:\trabajo\"
Call Mostrar_Archivos(Ruta)
Application.ScreenUpdating = True
MsgBox "Fin"
End Sub
'
Sub Mostrar_Archivos(Ruta)
libro = ThisWorkbook.Name
hoja = "B-Ends"
wruta = Ruta
'
Dim fs As Object, carpeta As Object, archivo As Object, subcarpeta As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set carpeta = fs.GetFolder(wruta)
'
If wruta = "" Then
Exit Sub
ElseIf Right(wruta, 1) <> "\" Then
wruta = wruta & "\"
End If
For Each archivo In carpeta.Files
If LCase(Right(archivo.Name, 4)) = "xlsm" Then
FileName = wruta & archivo.Name
Call copia(FileName)
End If
Next
'
For Each subcarpeta In carpeta.subfolders
Call Mostrar_Archivos(subcarpeta)
Next
End Sub
'
Sub copia(FileName)
'Act.Por.Dante Amor
'
Dim Fila As Integer
'
'Libro = ThisWorkbook.Name
Set l1 = ThisWorkbook
hoja = "B-Ends"
Set h1 = l1.Sheets(hoja)
Fila = h1.Range("A" & Rows.Count).End(xlUp).Row + 1
'
Set l2 = Workbooks.Open(FileName)
'Revisa hoja
existe = False
For Each h In l2.Sheets
If LCase(h.Name) = LCase(hoja) Then
existe = True
Exit For
End If
Next
If existe Then
Set h2 = l2.Sheets(hoja)
'Copia datos
u2 = h2. Cells(Rows. Count, 1).End(xlUp). Row
h2. Range(h2.Cells(9, "A"), h2. Cells(u2, 176)). Copy
h1. Cells(Fila, "A").PasteSpecial xlValues
u1 = h1. Cells(Rows. Count, 1).End(xlUp). Row
h1. Range(h1. Cells(Fila, 22), h1. Cells(u1, 22)).Value = FileName
End If
Application.DisplayAlerts = False
l2.Close False
End Sub
.
'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias
.
Avísame cualquier duda
.