Advertisement
YasserKhalil2019

T4088_Loop Through Tables Collect Data To Master Sheet

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