Sub PMWkBk_Creator()
'/// this macro takes a master workbook and splits it out by specific Initials in G4
'/// relies on project sheets to be bound by "First" and "Last"
'/// partners with the BACC Update to Master macro.
Dim Sourcewb, Destwb, wkbPH, wkbMT As Workbook
Dim wkbNV, wkbLM, wkbPA, wkbLH, wkbEM As Workbook
Dim IncludedSheets()
Dim x()
ReDim x(0)
Dim startt As Integer, endd As Integer
Dim i As Integer
Dim fname As String, wklyupdatefolder As String, WklyPMMailout As String
Dim c As Variant
Dim codeFromSource As String
With Application
.DisplayAlerts = False
' .EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
Set Sourcewb = ThisWorkbook
'/// Export vba Modules necessary to run BACC file
With ThisWorkbook
.VBProject.VBComponents("ThisWorkbook").Export ("C:\BACC_ThisWkBk.cls")
.VBProject.VBComponents("module1").Export ("C:\BACC_MOD1.bas")
.VBProject.VBComponents("module2").Export ("C:\BACC_MOD2.bas")
.VBProject.VBComponents("module3").Export ("C:\BACC_MOD3.bas")
.VBProject.VBComponents("module4").Export ("C:\BACC_MOD4.bas")
.VBProject.VBComponents("module6").Export ("C:\BACC_MOD6.bas")
End With
'///declare array of necessary REPORT sheets
IncludedSheets = Array("SAVINGS ACCUMULATOR", "Sheet Index", "Total Savings Table", "First", "Last", _
"Project Inception Form", "Sourcing Parameters", "CODES, LOOKUPS, ETC", _
"Projects Summary", "WIP", "Projects Phasing Summary")
With Sourcewb
'///name bookend sheets
startt = Worksheets("First").Index + 1
endd = Worksheets("Last").Index - 1
End With
'///come back to source file
For Each c In Range("PMs")
If c.Value = "" Then GoTo AfterArrays
'MsgBox c.Value
Sourcewb.Activate
For i = startt To endd
'///G4 holds PM initials
If Sheets(i).Range("g4") = c Then
x(UBound(x)) = Sheets(i).Name
ReDim Preserve x(UBound(x) + 1)
End If
Next i
ReDim Preserve x(UBound(x) - 1)
'///add new workbook
Sheets(IncludedSheets).Copy
Set Destwb = ActiveWorkbook
Application.Run "importVBA"
With Sourcewb.VBProject.VBComponents("ThisWorkbook").CodeModule
codeFromSource = .Lines(1, .CountOfLines)
End With
With Destwb.VBProject.VBComponents("ThisWorkbook").CodeModule
.DeleteLines 1, .CountOfLines
.AddFromString codeFromSource
End With
fname = Format(Now, "dd-mmm-yy") & " - " & c & " - " & Sourcewb.Name
wklyupdatefolder = "Z:\REPORTS\006 Project Calculations\Proj Calculations 2009\Weekly Updates from PMs"
WklyPMMailout = wklyupdatefolder & "\" & fname
Destwb.SaveAs Filename:=WklyPMMailout
'///change to copying both arrays to new book
Destwb.Sheets("First").Visible = True
Sourcewb.Sheets(x).Copy After:=Destwb.Worksheets("First")
'///direct any links created when copying formulas back to the BACC
Destwb.ChangeLink Name:=Sourcewb.Name _
, NewName:=Destwb.Name, Type:= _
xlExcelLinks
ActiveWorkbook.Close (True)
ReDim x(0)
Next c
AfterArrays:
'///still need to suppress range name warnings when the new c is picked up on each loop???
With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Sub importVBA()
'///import necessary macros to new workbooks
With Application.VBE.ActiveVBProject
.VBComponents.Import ("C:\BACC_MOD1.bas")
.VBComponents.Import ("C:\BACC_MOD2.bas")
.VBComponents.Import ("C:\BACC_MOD3.bas")
.VBComponents.Import ("C:\BACC_MOD4.bas")
.VBComponents.Import ("C:\BACC_MOD6.bas")
End With
End Sub