Advertisement
YasserKhalil2019

T3876_Export Sheets One Workbook CopyModule Each Firm

Sep 9th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. https://excel-egy.com/forum/t3876
  2. ---------------------------------
  3.  
  4. Sub Export_Sheets_One_Workbook_CopyModule_Each_Firm()
  5. Dim sh As Worksheet
  6. Dim ws As Worksheet
  7. Dim wk As Worksheet
  8. Dim arrSheets As Variant
  9. Dim aData As Variant
  10. Dim c As Range
  11. Dim sSheets() As String
  12. Dim n As Long
  13.  
  14. Application.ScreenUpdating = False
  15. Set sh = ThisWorkbook.Worksheets("DATA")
  16. arrSheets = Array("DATA", "Sheet3", "Sheet5", "Sheet8", "Sheet10")
  17.  
  18. With sh
  19. With .Range("K8", .Range("K" & Rows.Count).End(xlUp))
  20. .AdvancedFilter 1, , , True
  21. .Offset(1).Copy .Parent.Range("CD1")
  22. End With
  23. .ShowAllData
  24. End With
  25.  
  26. For Each ws In Worksheets(arrSheets)
  27. n = n + 1
  28. ReDim Preserve sSheets(1 To n)
  29. sSheets(n) = ws.Name
  30. Next ws
  31.  
  32. Application.DisplayAlerts = False
  33. For Each c In sh.Range("CD1:CD" & sh.Cells(Rows.Count, "CD").End(xlUp).Row)
  34. If c.Value <> "" Then
  35. Worksheets(sSheets).Copy
  36. With ActiveWorkbook
  37. Set wk = .Worksheets("DATA")
  38. .SaveAs Filename:=ThisWorkbook.Path & "\Output_" & c.Value, FileFormat:=52
  39.  
  40. With wk.Range("A7").CurrentRegion
  41. aData = .Offset(1).Value
  42. aData = FilterArray(aData, 11, CStr(c.Value), True)
  43. .Offset(1).ClearContents
  44. .Parent.Range("A8").Resize(UBound(aData, 1), UBound(aData, 2)).Value = aData
  45. End With
  46.  
  47. wk.Columns(82).Clear
  48. CopyModule ThisWorkbook, "Module2", ActiveWorkbook
  49.  
  50. For Each ws In ActiveWorkbook.Worksheets
  51. ws.UsedRange.Value = ws.UsedRange.Value
  52. Next ws
  53. .Close True
  54. End With
  55. End If
  56. Next c
  57.  
  58. sh.Columns(82).Clear
  59. Application.DisplayAlerts = True
  60.  
  61. Application.ScreenUpdating = True
  62.  
  63. MsgBox "Done...", 64
  64. End Sub
  65.  
  66. Sub CopyModule(sourceWB As Workbook, strModuleName As String, targetWB As Workbook)
  67. Dim strFolder As String
  68. Dim strTempFile As String
  69.  
  70. strFolder = sourceWB.Path
  71. If Len(strFolder) = 0 Then strFolder = CurDir
  72. strFolder = strFolder & "\"
  73.  
  74. strTempFile = strFolder & "~tmpexport.bas"
  75.  
  76. On Error Resume Next
  77. sourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
  78. targetWB.VBProject.VBComponents.Import strTempFile
  79. Kill strTempFile
  80. On Error GoTo 0
  81. End Sub
  82.  
  83. Function FilterArray(ByVal myRefArr As Variant, ByVal col As Integer, ByVal refValue As String, ByVal equal As Boolean) As Variant
  84. Dim i As Long
  85. Dim j As Long
  86. Dim n As Long
  87.  
  88. On Error Resume Next
  89. n = 1
  90.  
  91. If refValue = "" Then
  92. FilterArray = myRefArr
  93. Else
  94. ReDim a(1 To UBound(myRefArr, 1), 1 To UBound(myRefArr, 2))
  95. For i = 1 To UBound(a, 1)
  96. If IIf(equal, UCase(myRefArr(i, col)) = UCase(refValue), UCase(myRefArr(i, col)) <> UCase(refValue)) Then
  97. For j = 1 To UBound(a, 2)
  98. a(n, j) = myRefArr(i, j)
  99. Next j
  100. n = n + 1
  101. End If
  102. Next i
  103.  
  104. ReDim b(1 To n - 1, 1 To UBound(a, 2))
  105. For i = 1 To n - 1
  106. For j = 1 To UBound(a, 2)
  107. b(i, j) = a(i, j)
  108. Next j
  109. Next i
  110. FilterArray = b
  111. End If
  112. On Error GoTo 0
  113. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement