+ Reply to Thread
Results 1 to 5 of 5

Macro is deleting sheet, and I would like it to clear contents

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2009
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Macro is deleting sheet, and I would like it to clear contents

    I am using a borrowed macro to autofilter for me. The macro does exactly what I want it to, with one exception. It deletes and recreates a worksheet page everytime you run it. I think I would like it to just clear contents instead of delete. I am trying to copy the data from the filter to an invoice sheet, but it keeps returning a #REF! error because the sheet is deleted and recreated each time I run the macro. I tried changing the .delete to a .clearcontents, but then it has a problem with creating a sheet with a name that is already being used. I'm rather new at macros and need some help with this. Thanks....The macro is:
    Sub Copy_With_AutoFilter1()
        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("Order Sheet")  '<<< 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:C" & Rows.Count)
    
        'Firstly, remove the AutoFilter
        WS.AutoFilterMode = False
    
        'Delete the sheet MyFilterResult if it exists
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets("MyFilterResult").Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
    
        'Add a new worksheet to copy the filter results in
        Set WSNew = Worksheets.Add
        WSNew.Name = "MyFilterResult"
    
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        WS.AutoFilter.Range.Copy
        With WSNew.Range("A1")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
            .Select
        End With
        
        'Close AutoFilter
        WS.AutoFilterMode = False
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    
    End Sub
    Last edited by royUK; 02-09-2009 at 03:36 PM.

  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: Macro is deleting sheet, and I would like it to clear contents

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    I will add them this time but read the rules before you post again
    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 Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro is deleting sheet, and I would like it to clear contents

    You must have the sheet("MyFilterResult") to copy to
    Sub Copy_With_AutoFilter1()
        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("Order Sheet")  '<<< 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:C" & Rows.Count)
    
        'Firstly, remove the AutoFilter
        WS.AutoFilterMode = False
    
        'Delete the sheet MyFilterResult if it exists
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets("MyFilterResult").UsedRange.ClearContents
        Application.DisplayAlerts = True
        On Error GoTo 0
    
       
    
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        WS.AutoFilter.Range.Copy
        With WSNew.Range("A1")
            ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
            .PasteSpecial Paste:=8
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
            .Select
        End With
        
        'Close AutoFilter
        WS.AutoFilterMode = False
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    
    End Sub

  4. #4
    Registered User
    Join Date
    02-06-2009
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro is deleting sheet, and I would like it to clear contents

    Sorry about the code tags, this is all pretty new to me. I changed the .delete to .UsedRange.ClearContent, and I get the following error:
    "Cannot rename a sheet to the same name as another sheet, a referenced object library or workbook referenced by visual basic."

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro is deleting sheet, and I would like it to clear contents

    I don't think you copied all the relevant sections of Roy's code. You're still trying to create the worksheet again.

    It's a help to say what line the error occurs on.
    Last edited by shg; 02-09-2009 at 06:08 PM.
    Entia non sunt multiplicanda sine necessitate

+ 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