Advertisement
YasserKhalil2019

T4439_Loop Through Tables Collect Data To Master Sheet

Dec 21st, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. https://excel-egy.com/forum/t4439
  2. ---------------------------------
  3.  
  4. Sub Loop_Through_Tables_Collect_Data_To_Master_Sheet()
  5. Dim a, sh As Worksheet, tbl As ListObject, x As Byte, i As Long, m As Long, n As Long, lr As Long
  6.  
  7. Application.ScreenUpdating = False
  8. Set sh = ThisWorkbook.Worksheets(1)
  9. Set tbl = sh.ListObjects(1)
  10. If tbl.ListRows.Count >= 1 Then tbl.DataBodyRange.Delete
  11. sh.ListObjects(1).ListRows.Add
  12.  
  13. For i = 2 To 13 'Sheets Index
  14. With ThisWorkbook.Worksheets(i)
  15. m = LastRow(.ListObjects(1), 2)
  16. n = LastRow(sh.ListObjects(1), 2)
  17. If m <> -1 Then
  18. lr = IIf(n = -1, 3, n + 1)
  19. a = .Range("B3:E" & m).Value
  20. sh.ListObjects(1).DataBodyRange.Cells(lr - 2, 2).Resize(UBound(a, 1), UBound(a, 2)).Value = a
  21. End If
  22. End With
  23. Next i
  24.  
  25. If n <> -1 Then
  26. With sh.ListObjects(1).DataBodyRange.Cells(1).Resize(n - 2)
  27. .Value = Evaluate("ROW(1:" & .Count & ")")
  28. End With
  29. End If
  30. Application.ScreenUpdating = True
  31.  
  32. MsgBox "Done...", 64
  33. End Sub
  34.  
  35. Function LastRow(tbl As ListObject, col As Long)
  36. Dim rng As Range
  37.  
  38. On Error GoTo Skipper
  39. Set rng = tbl.ListColumns(col).DataBodyRange.Find(What:="*", SearchDirection:=xlPrevious)
  40. LastRow = rng.Row
  41. Exit Function
  42.  
  43. Skipper:
  44. LastRow = -1
  45. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement