SOLVED

Split a file based on filter on datarange

%3CLINGO-SUB%20id%3D%22lingo-sub-2694701%22%20slang%3D%22en-US%22%3ESplit%20a%20file%20based%20on%20filter%20on%20datarange%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2694701%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20file%20which%20has%20filler%20lines%20on%20top%20and%20bottom%20of%20a%20a%20defined%20datarange%2Fdataset.%26nbsp%3B%3CBR%20%2F%3EWhat%20I%20would%20like%20to%20do%20is%20run%20a%20macro%20or%20script%20to%20filter%20through%20each%20value%20in%20Column%20A%20(Leader%2C%20see%20attached%20file)%20of%20the%20named%20range%2C%20and%20then%20save%20each%20result%20in%20a%20new%20file%20(named%20Review-LeaderValue.xlsx%20for%20example%2C%20where%20the%20%22LeaderValue%22%20is%20the%20resulting%20value%20in%20the%20filter%20of%20column%20A).%3CBR%20%2F%3E%3CBR%20%2F%3EAlso%2C%20I'd%20like%20put%20the%20now%20unique%20value%20in%20the%20Leader%20Column%20into%20cell%20B1.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EI've%20attached%20an%20example%20file%20of%20how%20it%20looks%20-%20and%20it%20has%20two%20sheets.%20%22Dataset%22%20to%20show%20how%20the%20original%20would%20look%20and%20%22Example%20Outcome%22%20to%20show%20what%20I%20want%20to%20try%20to%20achieve.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20it%20was%20just%20a%20pure%20dataset%20it%20would%20be%20easier%20to%20accomodate%2C%20but%20as%20I%20need%20to%20retain%20the%20lines%20above%20and%20below%20the%20set%20I%20am%20a%20tad%20lost.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%C3%98ystein%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2694701%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2695255%22%20slang%3D%22en-US%22%3ERe%3A%20Split%20a%20file%20based%20on%20filter%20on%20datarange%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2695255%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1139977%22%20target%3D%22_blank%22%3E%40Oystein_Tvedten%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20you%20go%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20SplitToFiles()%0A%20%20%20%20Const%20h%20%3D%206%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'%20Header%20row%0A%20%20%20%20Dim%20ws%20As%20Worksheet%20%20%20%20%20%20%20'%20Source%20sheet%0A%20%20%20%20Dim%20r%20As%20Long%20%20%20%20%20%20%20%20%20%20%20%20%20'%20Row%20on%20source%20sheet%0A%20%20%20%20Dim%20m%20As%20Long%20%20%20%20%20%20%20%20%20%20%20%20%20'%20Last%20data%20row%0A%20%20%20%20Dim%20wb%20As%20Workbook%20%20%20%20%20%20%20%20'%20New%20workbook%0A%20%20%20%20Dim%20wt%20As%20Worksheet%20%20%20%20%20%20%20'%20New%20worksheet%0A%20%20%20%20Dim%20col%20As%20New%20Collection%20'%20Unique%20leaders%0A%20%20%20%20Dim%20v%20As%20Variant%20%20%20%20%20%20%20%20%20%20'%20Leader%0A%20%20%20%20Dim%20p%20As%20String%20%20%20%20%20%20%20%20%20%20%20'%20Path%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20p%20%3D%20ThisWorkbook.Path%20%26amp%3B%20Application.PathSeparator%0A%20%20%20%20Set%20ws%20%3D%20ActiveSheet%0A%20%20%20%20m%20%3D%20ws.Range(%22A%22%20%26amp%3B%20h).End(xlDown).Row%0A%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20For%20r%20%3D%20h%20%2B%201%20To%20m%0A%20%20%20%20%20%20%20%20col.Add%20Item%3A%3Dws.Range(%22A%22%20%26amp%3B%20r).Value%2C%20Key%3A%3Dws.Range(%22A%22%20%26amp%3B%20r).Value%0A%20%20%20%20Next%20r%0A%20%20%20%20On%20Error%20GoTo%200%0A%20%20%20%20For%20Each%20v%20In%20col%0A%20%20%20%20%20%20%20%20ws.Copy%0A%20%20%20%20%20%20%20%20Set%20wb%20%3D%20ActiveWorkbook%0A%20%20%20%20%20%20%20%20Set%20wt%20%3D%20wb.Worksheets(1)%0A%20%20%20%20%20%20%20%20wt.Range(%22B1%22).Value%20%3D%20v%0A%20%20%20%20%20%20%20%20wt.Range(%22A%22%20%26amp%3B%20h).CurrentRegion.AutoFilter%20Field%3A%3D1%2C%20Criteria1%3A%3D%22%26lt%3B%26gt%3B%22%20%26amp%3B%20v%0A%20%20%20%20%20%20%20%20wt.Range(%22A%22%20%26amp%3B%20h%20%2B%201%20%26amp%3B%20%22%3AA%22%20%26amp%3B%20m).EntireRow.Delete%0A%20%20%20%20%20%20%20%20wt.Range(%22A%22%20%26amp%3B%20h).CurrentRegion.AutoFilter%0A%20%20%20%20%20%20%20%20wb.SaveAs%20Filename%3A%3Dp%20%26amp%3B%20v%20%26amp%3B%20%22.xlsx%22%2C%20FileFormat%3A%3DxlOpenXMLWorkbook%0A%20%20%20%20%20%20%20%20wb.Close%20SaveChanges%3A%3DFalse%0A%20%20%20%20Next%20v%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2697653%22%20slang%3D%22en-US%22%3ERe%3A%20Split%20a%20file%20based%20on%20filter%20on%20datarange%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2697653%22%20slang%3D%22en-US%22%3EPerfect!%20Thanks%20a%20bunch!%20%3CLI-EMOJI%20id%3D%22lia_grinning-face-with-smiling-eyes%22%20title%3D%22%3Agrinning_face_with_smiling_eyes%3A%22%3E%3C%2FLI-EMOJI%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi,

 

I have a file which has filler lines on top and bottom of a a defined datarange/dataset. 
What I would like to do is run a macro or script to filter through each value in Column A (Leader, see attached file) of the named range, and then save each result in a new file (named Review-LeaderValue.xlsx for example, where the "LeaderValue" is the resulting value in the filter of column A).

Also, I'd like put the now unique value in the Leader Column into cell B1. 

I've attached an example file of how it looks - and it has two sheets. "Dataset" to show how the original would look and "Example Outcome" to show what I want to try to achieve. 

If it was just a pure dataset it would be easier to accomodate, but as I need to retain the lines above and below the set I am a tad lost. 

Øystein

2 Replies
best response confirmed by Oystein_Tvedten (New Contributor)
Solution

@Oystein_Tvedten 

Here you go:

Sub SplitToFiles()
    Const h = 6               ' Header row
    Dim ws As Worksheet       ' Source sheet
    Dim r As Long             ' Row on source sheet
    Dim m As Long             ' Last data row
    Dim wb As Workbook        ' New workbook
    Dim wt As Worksheet       ' New worksheet
    Dim col As New Collection ' Unique leaders
    Dim v As Variant          ' Leader
    Dim p As String           ' Path
    Application.ScreenUpdating = False
    p = ThisWorkbook.Path & Application.PathSeparator
    Set ws = ActiveSheet
    m = ws.Range("A" & h).End(xlDown).Row
    On Error Resume Next
    For r = h + 1 To m
        col.Add Item:=ws.Range("A" & r).Value, Key:=ws.Range("A" & r).Value
    Next r
    On Error GoTo 0
    For Each v In col
        ws.Copy
        Set wb = ActiveWorkbook
        Set wt = wb.Worksheets(1)
        wt.Range("B1").Value = v
        wt.Range("A" & h).CurrentRegion.AutoFilter Field:=1, Criteria1:="<>" & v
        wt.Range("A" & h + 1 & ":A" & m).EntireRow.Delete
        wt.Range("A" & h).CurrentRegion.AutoFilter
        wb.SaveAs Filename:=p & v & ".xlsx", FileFormat:=xlOpenXMLWorkbook
        wb.Close SaveChanges:=False
    Next v
    Application.ScreenUpdating = True
End Sub
Perfect! Thanks a bunch!