Forum Discussion
Excel File Split Macro
JoeCavasin I think this file is working please check it out
- Subodh_Tiwari_sktneerJun 27, 2020Silver Contributor
If you wish, you may directly format the date in the code instead of reading the formatted date from the cell if it is always today's date like this...
dt = Format(Date, "yyyy-mm-dd ")- JoeCavasinSep 03, 2020Brass Contributor
Thank you! the date will always be "today", works perfectly.
only other question - is it possible to have the sheet only make split copies for which there is data in column A? IE - the control sheet lists the 8 identifiers that may show up in column A, but not every run of this will have all 8 in column A. If on any given run only 2 or 3 of the identifiers are present, is it possible to have excel only split out to those two or three sheets, and not make all the other blank sheets?
Thanks
- Subodh_Tiwari_sktneerSep 04, 2020Silver Contributor
I have written the entire code from scratch. Please test it and let me know if that works fine on your end.
Sub Breakout() Dim wsTemplate As Worksheet Dim wsControl As Worksheet Dim wbReport As Workbook Dim Rng As Range Dim Cel As Range Dim lr As Long Dim tlr As Long Dim strFileName As String Dim strFilePath As String Dim dt As String Application.DisplayAlerts = False Application.ScreenUpdating = False Set wsTemplate = ThisWorkbook.Worksheets("Template") Set wsControl = ThisWorkbook.Worksheets("Control") If wsControl.Range("E1").Value = "" Then MsgBox "Please input the path for the destination file in E1 on Control Sheet first and then try again...", vbExclamation Exit Sub End If strFilePath = wsControl.Range("E1").Value If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & Application.PathSeparator If Dir(strFilePath, vbDirectory) = "" Then MsgBox "The folder " & strFilePath & " doesn't exist.", vbExclamation Exit Sub End If dt = Sheets("Control").Range("E2").Value wsTemplate.AutoFilterMode = False tlr = wsTemplate.Cells(Rows.Count, 1).End(xlUp).Row wsTemplate.Sort.SortFields.Clear wsTemplate.Range("A1").Sort key1:=wsTemplate.Range("A2"), order1:=xlAscending, Header:=xlYes lr = wsControl.Cells(Rows.Count, 1).End(xlUp).Row Set Rng = wsControl.Range("A2:A" & lr) For Each Cel In Rng If Cel.Value <> "" Then wsTemplate.Rows(1).AutoFilter field:=1, Criteria1:=Cel.Value If wsTemplate.Range("A1:A" & tlr).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then strFileName = dt & Cel.Value & ".xls" strFileName = strFilePath & strFileName wsTemplate.Copy Set wbReport = ActiveWorkbook wbReport.SaveAs Filename:=strFileName, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False wbReport.Close True End If End If Next Cel If wsTemplate.FilterMode Then wsTemplate.ShowAllData Application.DisplayAlerts = False Application.ScreenUpdating = True MsgBox "Task completed!", vbInformation End SubIn the attached, click the button called "Create Reports" on Control Sheet to run the code.