Consulting

Results 1 to 3 of 3

Thread: Create multiple workbooks from a filtered list

  1. #1
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location

    Create multiple workbooks from a filtered list

    Hi Everyone ,

    I am trying to create multiple workbooks from a list based on filtered data in column B. There are 56 unique items listed on the filter so I need 56 separate workbooks. I am using below macro which does the job but I need to make it dynamic to apply for all filtered items in one go. Can anyone help on how to do this ?

    Cheers
    B.


    Criteria1:= "X"
    ActiveCell.FormulaR1C1 = "X"
    "C:\Users\vbax\X.xlsx"




    Range("B3:B4").Select
        Selection.AutoFilter
        Range("B4").Select
        ActiveSheet.Range("$B$3:$B$1045").AutoFilter Field:=1, Criteria1:= _
            "X"
        Cells.Select
        Selection.Copy
        Workbooks.Add
        Cells.Select
        ActiveSheet.Paste
        Columns("A:Z").Select
        Columns("A:Z").EntireColumn.AutoFit
        Range("B4").Select
        Application.CutCopyMode = False
        ActiveCell.FormulaR1C1 = "X"
        ActiveWorkbook.SaveAs Filename:= _
            "C:\Users\vbax\X.xlsx" _
            , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        Range("A4").Select
        ActiveWindow.Close
    End Sub
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I took your Macro code, removed all the Macro Recorder added redundancy and made it a Procedure (Subroutine or Function.) Then I Gave it an Input Parameter so you can use another Subroutine to gather the 56 values and feed them to this Sub one at a time in a loop.
    Sub SamT(FName As String)
    
    With ThisWorkBook.Sheets("X") 'Edit as needed
    .Range("B3:B4").AutoFilter 'Note dot before Range. Means Range is on (With) Sheets("X")
    .Range("$B$3:$B$1045").AutoFilter Field:=1, Criteria1:=FName
    .Cells.Copy
    End With
    
    Workbooks.Add
    ActiveSheet.Range("A1").Paste
    Application.CutCopyMode = False
    Range("B4").Value = FName
    Columns("A:Z").AutoFit
    
    ActiveWorkbook.SaveAs Filename:= _
    "C:\Users\vbax\" & FName & ".xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    Thanks very much for your reply SamT however it gave an error regarding the SaveAs Method and I couldn't use it. I spent time on it to work it out but did end up creating 56 workbook manually :o)
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •