Hello team,
I have a macros that splits an excel file into several files based on the name of a manager in the first column. All is good, but the resulting files do not keep the widths of the columns and eventually the look is messy. Can my macros be updated to keep the original columns format? Thank you very much for your help!!! Here it is:
Option Explicit
Sub SplitIntoSeperateFiles()
Dim OutBook As Workbook
Dim DataSheet As Worksheet, OutSheet As Worksheet
Dim FilterRange As Range
Dim UniqueNames As New Collection
Dim LastRow As Long, LastCol As Long, _
NameCol As Long, Index As Long
Dim OutName As String
'set references and variables up-front for ease-of-use
Set DataSheet = ThisWorkbook.Worksheets("Sheet1")
NameCol = 1
LastRow = DataSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = DataSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set FilterRange = Range(DataSheet.Cells(1, NameCol), DataSheet.Cells(LastRow, LastCol))
'loop through the name column and store unique names in a collection
For Index = 2 To LastRow
On Error Resume Next
UniqueNames.Add Item:=DataSheet.Cells(Index, NameCol), Key:=DataSheet.Cells(Index, NameCol)
On Error GoTo 0
Next Index
'iterate through the unique names collection, writing
'to new workbooks and saving as the group name .xls
Application.DisplayAlerts = False
For Index = 1 To UniqueNames.Count
Set OutBook = Workbooks.Add
Set OutSheet = OutBook.Sheets(1)
With FilterRange
.AutoFilter Field:=NameCol, Criteria1:=UniqueNames(Index)
.SpecialCells(xlCellTypeVisible).Copy OutSheet.Range("A1")
End With
OutName = ThisWorkbook.FullName
OutName = Left(OutName, InStrRev(OutName, ""))
OutName = OutName & UniqueNames(Index)
OutBook.SaveAs Filename:=OutName, FileFormat:=xlExcel8, Password:="merit2019"
OutBook.Close SaveChanges:=False
Call ClearAllFilters(DataSheet)
Next Index
Application.DisplayAlerts = True
End Sub
'safely clear all the filters on data sheet
Sub ClearAllFilters(TargetSheet As Worksheet)
With TargetSheet
TargetSheet.AutoFilterMode = False
If .FilterMode Then
.ShowAllData
End If
End With
End Sub
Try modifying the copy/paste section to this:
With FilterRange
.AutoFilter Field:=NameCol, Criteria1:=UniqueNames(Index)
.SpecialCells(xlCellTypeVisible).Copy
With OutSheet.Range("A1")
.PasteSpecial xlPasteAll
.PasteSpecial xlPasteColumnWidths
End With
End With
Hi Velouria,
thank you for your help. The addition/ insert didn't work for me, when I copied/ replaced as is it was only keeping the information in the first criteria split file, in the next criteria files it was returning no data. However, I am very new in macros, and I might not notice some nuances that I had to deal with. Can I kindly ask you to put all the code together?
Thank you soo much and have a wonderful day
Anna
This should be the same effect as your original code but pasting column widths too:
Sub SplitIntoSeperateFiles()
Dim OutName As String
OutName = ThisWorkbook.FullName
OutName = Left(OutName, InStrRev(OutName, ""))
'set references and variables up-front for ease-of-use
Dim DataSheet As Worksheet
Set DataSheet = ThisWorkbook.Worksheets("Sheet1")
Dim NameCol As Long
NameCol = 1
Dim LastRow As Long
LastRow = DataSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim LastCol As Long
LastCol = DataSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Dim FilterRange As Range
Set FilterRange = Range(DataSheet.Cells(1, NameCol), DataSheet.Cells(LastRow, LastCol))
'loop through the name column and store unique names in a collection
Dim UniqueNames As Collection
Set UniqueNames = New Collection
Dim Index As Long
For Index = 2 To LastRow
On Error Resume Next
UniqueNames.Add Item:=DataSheet.Cells(Index, NameCol), Key:=DataSheet.Cells(Index, NameCol)
On Error GoTo 0
Next Index
'iterate through the unique names collection, writing
'to new workbooks and saving as the group name .xls
Application.DisplayAlerts = False
For Index = 1 To UniqueNames.Count
Dim OutBook As Workbook
Set OutBook = Workbooks.Add
Dim OutSheet As Worksheet
Set OutSheet = OutBook.Sheets(1)
With FilterRange
.AutoFilter Field:=NameCol, Criteria1:=UniqueNames(Index)
.SpecialCells(xlCellTypeVisible).Copy
With OutSheet.Range("A1")
.PasteSpecial xlPasteAll
.PasteSpecial xlPasteColumnWidths
End With
End With
OutBook.SaveAs Filename:=OutName & UniqueNames(Index), FileFormat:=xlExcel8, Password:="merit2019"
OutBook.Close SaveChanges:=False
Call ClearAllFilters(DataSheet)
Next Index
Application.DisplayAlerts = True
End Sub
'safely clear all the filters on data sheet
Sub ClearAllFilters(TargetSheet As Worksheet)
With TargetSheet
TargetSheet.AutoFilterMode = False
If .FilterMode Then
.ShowAllData
End If
End With
End Sub