Forum

Macro Split Files -...
 
Notifications
Clear all

Macro Split Files - Add back header rows

4 Posts
2 Users
0 Reactions
104 Views
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

I have  the macro to split the data into separte file but not sure how to add back the header rows [ ie rows 1 to 7]  to these Separate files.

Pls help to modify the Split Macro to put back these rows [ 1 to 7 ] into each separate files, to make files  more comprehensible.

 
Posted : 23/02/2017 12:02 am
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

file attached

 
Posted : 23/02/2017 1:24 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi David

See if this helps. Those in red are add/changed by me.

I have to copy the header from the "Header" worksheet as the header from your active sheet (MFC FC OUTPUT) is hidden by the advanced filter and cannot be copied.

 

Sub Generate()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

iCol = 4 '### Define your criteria column
strOutputFolder = "SMTL_FC" '### Define your path of output folder

Set Wb = ActiveWorkbook
Set ws = ThisWorkbook.ActiveSheet '### Don't edit below this line
Set rngLast = Columns(iCol).Find("*", Cells(1, iCol), , , xlByColumns, xlPrevious)
ws.Columns(iCol).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUnique = Range(Cells(2, iCol), rngLast).SpecialCells(xlCellTypeVisible)

If Dir(strOutputFolder, vbDirectory) = vbNullString Then MkDir strOutputFolder
For Each strItem In rngUnique
If strItem <> "" Then
ws.UsedRange.AutoFilter Field:=iCol, Criteria1:=strItem.Value
Workbooks.Add
ws.UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=[A7]
Wb.Worksheets("Header").Range("A1:J7").Copy Destination:=[A1]
strFilename = strOutputFolder & "" & strItem
Cells.Select
Selection.Columns.AutoFit
ActiveWorkbook.SaveAs Filename:=strFilename & "_Fcst", FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close savechanges:=False
End If
Next
ws.ShowAllData
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Selection.AutoFilter
End Sub

 
Posted : 23/02/2017 10:51 am
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

Thanks Sunny, your clever tricks in the macro to set the Header as a tool to compelte the VB loop a lot easier. It exactly fits the purpose.

Thanks millions again!!

 
Posted : 23/02/2017 8:18 pm
Share: