+ Reply to Thread
Results 1 to 11 of 11

VBA, filtering, formatting and saving various formatted versions to individual files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    VBA, filtering, formatting and saving various formatted versions to individual files

    Hi All,

    I'm looking to automate a recurring monthly task.

    Process:
    1) Filter original data (tab 1) by colour Column "D" then priority Column "C". Reds first then Amber then Green with colours to be individually ranked by project priority 1's first then 2's, 3, 4, 5 etc.

    I've added a filtered data tab (tab 2) to show how the filtered result should look.

    2) I then need to save down 6 individual copies of this data filtered by department Column "B". The filename should be called the department name and should only contain that particular departments information.

    To illustrate i've filtered by the "Operations" department in the 3rd tab. I'd then need to save down this tab only to a new file called "Operations.xlsm"

    Hope this makes sense.

    I can do this manually simply enough but going forward there will be many more entries and more departments making it quite time consuming.

    Regards
    Neill
    Attached Files Attached Files
    Last edited by Gti182; 03-25-2013 at 06:06 AM.

  2. #2
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    i've used this code to filter the data by colour and priority but having trouble coding the filtered department data to save to a new file.

    Sub Filter()
    '
    ' Filter by colour then by priority
    '
    
        Selection.AutoFilter
        ActiveWorkbook.Worksheets("Consolidated Poject RAG").AutoFilter.Sort.SortFields _
            .Clear
        ActiveWorkbook.Worksheets("Consolidated Poject RAG").AutoFilter.Sort.SortFields _
            .Add(Range("D3:D18"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
            SortOnValue.Color = RGB(255, 0, 0)
        ActiveWorkbook.Worksheets("Consolidated Poject RAG").AutoFilter.Sort.SortFields _
            .Add(Range("D3:D18"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
            SortOnValue.Color = RGB(255, 192, 0)
        ActiveWorkbook.Worksheets("Consolidated Poject RAG").AutoFilter.Sort.SortFields _
            .Add(Range("D3:D18"), xlSortOnCellColor, xlAscending, , xlSortNormal). _
            SortOnValue.Color = RGB(0, 176, 80)
        ActiveWorkbook.Worksheets("Consolidated Poject RAG").AutoFilter.Sort.SortFields _
            .Add Key:=Range("C3:c18"), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Consolidated Poject RAG").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Last edited by Gti182; 03-22-2013 at 12:39 PM.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    Hi Gti182

    This Code is in the attached and appears to do as you require
    Option Explicit
    Sub Do_Stuff()
        Dim wb As Workbook
        Dim newBook As Workbook
        Dim ws As Worksheet
        Dim LR As Long
        Dim cel As Range
        Dim myPath As String
    
        myPath = ThisWorkbook.Path & "\"
    
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Original")
    
        Application.ScreenUpdating = False
        If Not Evaluate("ISREF(Lists!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Lists"
        Else
            Sheets("Lists").Cells.Clear
        End If
    
        With ws
            LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Sort.SortFields.Clear
            .Sort.SortFields.Add(Range("D2:D" & LR), _
                    xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
            .Sort.SortFields.Add(Range("D2:D" & LR), _
                    xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 176, 80)
            .Sort.SortFields.Add Key:=Range("C2:C" & LR), _
                    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
                .SetRange Range("A2:D" & LR)
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
    
            .Columns(2).Copy Sheets("Lists").Range("A1")
            Sheets("Lists").Columns("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
            ActiveWorkbook.Names.Add Name:="Depts", RefersTo:= _
                    "=OFFSET(Lists!$A$2,0,0,(COUNTA(Lists!$A:$A)-1),1)"
    
            For Each cel In Range("Depts")
                .Range("A1:D" & LR).AutoFilter Field:=2, Criteria1:=cel
    
                Set newBook = Workbooks.Add(xlWBATWorksheet)
    
                With newBook
                    With ws.Range("A1:D" & LR)
                        .Copy
                        newBook.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
                        newBook.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteAll
                        newBook.Sheets(1).Name = cel
                    End With
    
                    Application.DisplayAlerts = False
                    .SaveAs myPath & cel, FileFormat:=51
                    Application.DisplayAlerts = True
                    .Close False
                End With
            Next cel
            .AutoFilterMode = False
            Application.DisplayAlerts = False
            Sheets("Lists").Delete
            Application.DisplayAlerts = True
        End With
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    That's! That works amazingly! Really appreciate it!

  5. #5
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    Hi John,

    dropped you a message. If i add an additional row to the top of the "original" tab how would this affect the code?

    Regards
    Neill

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    Hi Gti182

    If i add an additional row to the top of the "original" tab how would this affect the code
    It depends on how the new Row is populated and/or formated...show me.

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    Hi Gti182

    The attached workbook dumps the Mergerd Cells and uses Center Across Selection in it's place.
    Let me know of issues.
    Option Explicit
    Sub Do_Stuff()
        Dim wb As Workbook
        Dim newBook As Workbook
        Dim ws As Worksheet
        Dim LR As Long
        Dim cel As Range
        Dim myPath As String
    
        myPath = ThisWorkbook.Path & "\"
    
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Original")
    
        Application.ScreenUpdating = False
        If Not Evaluate("ISREF(Lists!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Lists"
        Else
            Sheets("Lists").Cells.Clear
        End If
    
        With ws
            LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Sort.SortFields.Clear
            .Sort.SortFields.Add(Range("D3:D" & LR), _
                    xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
            .Sort.SortFields.Add(Range("D3:D" & LR), _
                    xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 176, 80)
            .Sort.SortFields.Add Key:=Range("C3:C" & LR), _
                    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
                .SetRange Range("A3:D" & LR)
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
    
            .Columns(2).Copy Sheets("Lists").Range("A1")
            Sheets("Lists").Columns("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
            ActiveWorkbook.Names.Add Name:="Depts", RefersTo:= _
                    "=OFFSET(Lists!$A$2,0,0,(COUNTA(Lists!$A:$A)-1),1)"
    
            For Each cel In Range("Depts")
                .Range("A3:D" & LR).AutoFilter Field:=2, Criteria1:=cel
    
                Set newBook = Workbooks.Add(xlWBATWorksheet)
    
                With newBook
                    With ws.Range("A1:D" & LR)
                        .Copy
                        newBook.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
                        newBook.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteAll
                        newBook.Sheets(1).Name = cel
                    End With
    
                    Application.DisplayAlerts = False
                    .SaveAs myPath & cel, FileFormat:=51
                    Application.DisplayAlerts = True
                    .Close False
                End With
            Next cel
            .AutoFilterMode = False
            Application.DisplayAlerts = False
            Sheets("Lists").Delete
            Application.DisplayAlerts = True
        End With
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    The extra row added to the top of the sheet is formatted as text. In the new row A1-C1 will be a merged cell called "Project information"

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    Hi Gti182

    This is what I was concerned about
    In the new row A1-C1 will be a merged cell called "Project information"
    Show me the new file and I'll play with it...merged cells cause nothing but frustration...we'll see what we can do.

  10. #10
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    Thanks John that works beautifully. I've tested it with and without merged cells and they both work with above code. thanks again!

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA, filtering, formatting and saving various formatted versions to individual files

    You're welcome...glad I could help. Thanks for the Rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1