Hice esta macro para tomar los datos de una tablas ubicadas en muchas pestañas de un solo Archivo(A) a otro archivo (B).
sin embargo al momento de yo intentar usar vlookup tengo un error indicando "Application-defined or object-defined error"
He colocado varias mensajes de alerta para asegurarme que el rango sea el correcto, he incluso selecciono las celdas pero no logro corregirlo, algun consejo??
Private Sub macro()
On Error Resume Next
Dim Cont As Integer, rango As Range
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, J As Integer, lnCol As Long, lnRow As Long
Dim rcol As Integer
Dim result As Long
Dim LV As String
Dim FoundRange, rng As Range
Dim HG As Long
Application.ScreenUpdating = False
'Seleccionar folder o directorio donde se encuentra Reporte
directory = "C:\Users\edgar.soto\Desktop\Horas Barmachine Inline\LOB Report\"
fileName = Dir(directory & "*.xl??") '<-- you can use * and ? wild cards here.
Do While fileName <> ""
'Se abre reporte del directorio colocado
Workbooks.Open (directory & fileName)
fileName = Dir()
'ciclo FOR para tomar informacion de las ultimas 40 hojas del documento
For i = Workbooks(2).Sheets.Count - 40 To Workbooks(2).Sheets.Count
' MsgBox Workbooks(2).Sheets(i).Name
Set FoundRange = Workbooks(2).Sheets(i).Cells.Find("PJZILBMMK01", MatchCase:=False)
'MsgBox FoundRange.Address
fecha = DateAdd("d", 0, Workbooks(2).Sheets(i).Name)
' MsgBox FoundRange.Row & ", " & FoundRange.Column
lnRow = FoundRange.Row - 1
lnCol = Workbooks(2).Sheets(i).Cells(lnRow, 1).EntireRow.Find(What:="Sum of GainedHours", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
rcol = lnCol - FoundRange.Column + 1
Workbooks(1).Sheets("Data").Cells(1, 2 + Cont).Value = fecha
For J = 2 To 25
' Set rng = FoundRange.Resize(20, rcol)
Set rng = Workbooks(2).Sheets(i).Range("G53:M77")
'Set rng = Workbooks(2).ActiveSheet.Range("A1:J4")
Set Table_Range = Workbooks(2).Sheets(i).Range("B4:F11")
rng.Select
LV = Workbooks(1).Sheets("Data").Cells(J, 1).Value
result = Application.WorksheetFunction.VLookup(LV, rng, lnCol, False)
'Application.WorksheetFunction.VLookup(name, myrange, 2, False)
Next J
'MsgBox FoundRange.Address
Cont = Cont + 1
Next i
Loop
'Range(Cells(FoundRange.Row, FoundRange.Column), Cells(FoundRange.Row + 25))
End Sub
On Error Resume Next
al principio de tu código. Deberías eliminarlo y depurarlo sin ese comando. Respecto a VLOOKUP, comrpueba el valor derng
, deLV
y delnCol
cuando te da error. Recuerda que puedes depurar con F8