Advertisement
YasserKhalil2019

T3874_Export Specific Sheets One Workbook Arrays CopyModule

Sep 8th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. https://excel-egy.com/forum/t3874
  2. ---------------------------------
  3.  
  4. Sub Export_Specific_Sheets_To_One_Workbook_Using_Arrays_CopyModule()
  5. Dim ws As Worksheet
  6. Dim sSheets() As String
  7. Dim n As Long
  8.  
  9. Application.ScreenUpdating = False
  10. For Each ws In Worksheets(Array("Sheet1", "Sheet4"))
  11. n = n + 1
  12. ReDim Preserve sSheets(1 To n)
  13. sSheets(n) = ws.Name
  14. Next ws
  15.  
  16. Worksheets(sSheets).Copy
  17. Application.DisplayAlerts = False
  18. ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\Output", FileFormat:=52
  19. CopyModule ThisWorkbook, "Module2", ActiveWorkbook
  20. Application.DisplayAlerts = True
  21.  
  22. For Each ws In ActiveWorkbook.Worksheets
  23. ws.UsedRange.Value = ws.UsedRange.Value
  24. Next ws
  25.  
  26. ActiveWorkbook.Close True
  27. Application.ScreenUpdating = True
  28.  
  29. MsgBox "Done...", 64
  30. End Sub
  31.  
  32. Sub CopyModule(sourceWB As Workbook, strModuleName As String, targetWB As Workbook)
  33. Dim strFolder As String
  34. Dim strTempFile As String
  35.  
  36. strFolder = sourceWB.Path
  37. If Len(strFolder) = 0 Then strFolder = CurDir
  38. strFolder = strFolder & "\"
  39. strTempFile = strFolder & "~tmpexport.bas"
  40.  
  41. On Error Resume Next
  42. sourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
  43. targetWB.VBProject.VBComponents.Import strTempFile
  44. Kill strTempFile
  45. On Error GoTo 0
  46. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement