+ Reply to Thread
Results 1 to 9 of 9

Modification of macro to clear not delete page.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-15-2008
    Location
    Oxfordshire, UK
    MS-Off Ver
    2007.
    Posts
    100

    Modification of macro to clear not delete page.

    Hi All,

    I have a macro that I found on the net for copying a filtered selection and copying to a new sheet. Works great and thanks very much to the donor. I would like to alter it slightly so that it just clears the sheet called 'Interval tasks' instead of deleting it and creating a new one. Here is the code:

    Sub Copy_With_AutoFilter1() 'This code filters the task list by the true statement that is the outcome of the
                                'calender time selected by the task resource userform.  It then creates a new sheet
                                'called Interval Tasks
    
        Dim WS As Worksheet
        Dim WSNew As Worksheet
        Dim rng As Range
        Dim rng2 As Range
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'Name of the worksheet with the data
        Set WS = Sheets("All Tasks")  '<<< Change
    
        'Set filter range : A1 is the top left cell of your filter range and
        'the header of the first column, D is the last column in the filter range
        Set rng = WS.Range("A1:U" & Rows.Count)
    
        'Firstly, remove the AutoFilter
        WS.AutoFilterMode = False
    
        'Delete the sheet MyFilterResult if it exists
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets("Interval Tasks").Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
    
        'This example filters on the first column in the range (change the field if needed)
        'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
        'Use "<>Netherlands" if you want the opposite
        rng.AutoFilter Field:=21, Criteria1:="=True"
    
        'if you want to filter on a cell value you can use this, use "<>" for the opposite
        'This example uses the activecell value
        'rng.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value
    
        'This will use the cell value from A2
        'rng.AutoFilter Field:=1, Criteria1:="=" & WS.Range("A2").Value
    
        'Add a new worksheet to copy the filter results in
        Set WSNew = Worksheets.Add
        WSNew.Name = "Interval Tasks"
        WSNew.Move after:=ThisWorkbook.Sheets("Charts")
        
        Call FormatIntervals
        
    
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        WS.AutoFilter.Range.Copy
        With WSNew.Range("A2")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            .PasteSpecial xlPasteFormulas
            .Sort Key1:=Range("N2"), Order1:=xlAscending, Header:= _
            xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
            Application.CutCopyMode = False
            .Select
        End With
        'Close AutoFilter on original sheet
        WS.AutoFilterMode = False
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        Call SortandFilter
    End Sub

    I have had a couple fo attempts to do it but so far no luck. Can anyone help?

    Many thanks,
    JD
    Last edited by jad70; 02-28-2009 at 04:00 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Modification of macro to clear not delete page.

    Try this
    Sub Copy_With_AutoFilter1() 'This code filters the task list by the true statement that is the outcome of the
                                'calender time selected by the task resource userform.  It then creates a new sheet
                                'called Interval Tasks
    
        Dim WS As Worksheet
        Dim WSNew As Worksheet
        Dim rng As Range
        Dim rng2 As Range
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'Name of the worksheet with the data
        Set WS = Sheets("All Tasks")  '<<< Change
    
        'Set filter range : A1 is the top left cell of your filter range and
        'the header of the first column, D is the last column in the filter range
        Set rng = WS.Range("A1:U" & Rows.Count)
    
        'Firstly, remove the AutoFilter
        WS.AutoFilterMode = False
    
        'Delete the sheet MyFilterResult if it exists
       
        Set WSNew = Sheets("Interval Tasks").UsedRange
        WSNew.ClearContents
        
    
        'This example filters on the first column in the range (change the field if needed)
        'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
        'Use "<>Netherlands" if you want the opposite
        rng.AutoFilter Field:=21, Criteria1:="=True"
    
        'if you want to filter on a cell value you can use this, use "<>" for the opposite
        'This example uses the activecell value
        'rng.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value
    
        'This will use the cell value from A2
        'rng.AutoFilter Field:=1, Criteria1:="=" & WS.Range("A2").Value
        
        Call FormatIntervals
        
    
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        WS.AutoFilter.Range.Copy
        With WSNew.Range("A2")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            .PasteSpecial xlPasteFormulas
            .Sort Key1:=Range("N2"), Order1:=xlAscending, Header:= _
            xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
            Application.CutCopyMode = False
            .Select
        End With
        'Close AutoFilter on original sheet
        WS.AutoFilterMode = False
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        Call SortandFilter
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    12-15-2008
    Location
    Oxfordshire, UK
    MS-Off Ver
    2007.
    Posts
    100

    Re: Modification of macro to clear not delete page.

    Roy,

    Many thanks for the very quick reply.
    I am getting a compile error at this line

    WSNew.ClearContents
    Any suggestions?

    JD

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Modification of macro to clear not delete page.

    Should be

    WSNew.UsedRange.ClearContents

  5. #5
    Forum Contributor
    Join Date
    12-15-2008
    Location
    Oxfordshire, UK
    MS-Off Ver
    2007.
    Posts
    100

    Re: Modification of macro to clear not delete page.

    Hi Roy,

    I just found the exact same thing on another thread in this forum. You replied to this also, so I have copied in that code.
    It now stops here with error 91 and inserts the formatting on the Info sheet.

    With WSNew.Range("A2")
    I have attached the workbook. I want to insert the copied range at A2 as I have some script to put in A1. This is done with the 'format' macro which I will have to re-write. Basically only want the range that starts in A2 to be deleted on the sheet 'Interval Tasks'

    Many thanks,

    JD
    Attached Files Attached Files
    Last edited by jad70; 02-27-2009 at 07:24 AM. Reason: wrong file.

  6. #6
    Forum Contributor
    Join Date
    12-15-2008
    Location
    Oxfordshire, UK
    MS-Off Ver
    2007.
    Posts
    100

    Re: Modification of macro to clear not delete page.

    HI Roy,

    So, managed to track down the cause- WSNew was never set. (I think)
    But now it stops at another line of .Select

    Set WSNew = Sheet4
        Set rng2 = WSNew.Range("A2")
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        WS.AutoFilter.Range.Copy
        With WSNew.Range("A2")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            .PasteSpecial xlPasteFormulas
            Application.CutCopyMode = False
            .Select
        Call FormatIntervals
        Call SortandFilter
        End With
        
        'Close AutoFilter on original sheet
        WS.AutoFilterMode = False
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End Sub
    A bit confised as this all worked before.

    Thanks again.

    JD

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Modification of macro to clear not delete page.

    can you attach the workbook?

  8. #8
    Forum Contributor
    Join Date
    12-15-2008
    Location
    Oxfordshire, UK
    MS-Off Ver
    2007.
    Posts
    100

    Re: Modification of macro to clear not delete page.

    Certainly Roy.

    The macro is in module2. No matter what I do it still stops there. It was stopping in the sort selection that I have now moved to the code-sortfilter- but now it just stops at the .Select.
    If it all works the filtered range should be copied at A2 and there will be some script in row 1 (of sheet Interval tasks). I will alter this as it was written due to the sheet being re-generated.

    Many thanks.

    JD.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    12-15-2008
    Location
    Oxfordshire, UK
    MS-Off Ver
    2007.
    Posts
    100

    Re: Modification of macro to clear not delete page.

    Hi All,

    So, it would appear I have solved this using 'Advanced filter', copying the filtered data to another sheet, and it is so much easier.
    Here is the code:
    Sub Filter()
    'where- sheet3 is the sheet with the data you want to filter
            'Range("A1:U & Rows.Count") is the range containing the data
            'CriteriaRange is where you have set up your filter criteria
            'copy destination is where you want the filtered data to end up
        With Sheet3
    '
            .Range("A1:U" & Rows.Count).AdvancedFilter Action:=xlFilterInPlace, _
            CriteriaRange:=Sheet1.Range("D1:D2"), Unique:=False
    
            .Range("A1:U" & Rows.Count).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet30.Range _
            ("A2")
            .ShowAllData
        End With
    End Sub
    I hope this helps more people out as there seems to be a lot of people who are trying to do the same thing.
    I have also attached the workbook so you can see that it does indeed work.

    I have seen though some issues with Excel 2007 and this macro might not work. I am thinking of the future users of this database and would not like to fall into a big trap. Has anyone got experience of this?

    Anyway, feeling quite good to be able to contribute something at last.

    JD
    Attached Files Attached Files

+ 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