+ Reply to Thread
Results 1 to 12 of 12

Clear contents for filtered range only

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Clear contents for filtered range only

    Hello everyone
    I have the range("A1:A24") .. A1 has a header .. And A2:A24 has values
    I filtered this range so there are hidden cells ..
    How can I clear the contents of these hidden filtered cells without using loops
    As I know it could be done using loops and test if cell row is hidden then to clear the contents

    Simply I am searching for verse way as SpecialCells(xltypeVisible)
    Thanks advanced for help
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Clear contents for filtered range only

    Why don't to reverse your autofilter condition to show Non Matching Results and use Visiblecells property to clear the cells.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Clear contents for filtered range only

    That's a great idea .. but in fact it is not the purpose
    I need to set Rng for hidden cells as a result of filter ..

    Another point is it possible to clear the contents for the whole range ("A2:A24") (visible and Non-visible) without cancelling filter and without using loops

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Clear contents for filtered range only

    Check the below code to know how to clear autofilter visible cells.

    Sub ClearFilteredContents()
    
    With ActiveSheet
        .AutoFilterMode = False
        Set rFilter = .Range("$A$1").CurrentRegion
        rFilter.AutoFilter Field:=2, Criteria1:=1
        
        If Application.WorksheetFunction.Subtotal(3, rFilter.Columns(1)) > 1 Then
            Set rFilter = rFilter.Offset(1).Resize(rFilter.Rows.Count - 1)
            rFilter.SpecialCells(xlCellTypeVisible).ClearContents
        End If
        
        .AutoFilterMode = False
    End With
    
    End Sub
    Reverse the same logic Criteria1:=1 to Criteria1:="<>1" to get the autofilter hidden cells and clear the filtered data.

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Clear contents for filtered range only

    Thank you very much
    That a great step to reach the goal (half-way)

    Is it possible to reverse the criteria without knowing it ??
    I mean suppose the criteria was "<50" ... Is it possible to store this criteria without knowing it .... let excel vba store it and then reverse the action to complete the task

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Clear contents for filtered range only

    Sub test()
        Dim rng As Range
        With Range("a1", Range("a" & Rows.Count).End(xlUp))
            .AutoFilter 1, "<50"
            Set rng = .SpecialCells(12)
            .AutoFilter
            rng.EntireRow.Hidden = True
            .SpecialCells(12).EntireRow.Delete
            rng.EntireRow.Hidden = False
        End With
    End Sub

  7. #7
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Clear contents for filtered range only

    Here is the revised code with sample file

    Sub ClearFilteredContents()
    Dim sCrit As String, sReverse As String
    
    sCrit = InputBox("Enter Filter Value", "Filter Value Required", ">50")
    sReverse = IIf(InStr(1, sCrit, "<"), Replace(sCrit, "<", ">"), Replace(sCrit, ">", "<"))
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        .AutoFilterMode = False
        Set rFilter = .Range("$A$1").CurrentRegion
        rFilter.AutoFilter Field:=2, Criteria1:=sReverse
        
        If Application.WorksheetFunction.Subtotal(3, rFilter.Columns(1)) > 1 Then
            Set rFilter = rFilter.Offset(1).Resize(rFilter.Rows.Count - 1)
            rFilter.SpecialCells(xlCellTypeVisible).ClearContents
        End If
        
        rFilter.AutoFilter Field:=2, Criteria1:=sCrit
    End With
    
    Application.ScreenUpdating = True
    
    MsgBox "Autofilter Hidden Rows data removed successfully", vbInformation, "Remove Filter and Check"
    
    End Sub
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Clear contents for filtered range only

    Thank you very very much for these wonderful solutions ...
    In fact the codes are very great ..

    Can I ask for last request ..?
    Suppose I have the range filtered but I don't know the criteria applied to that range .. How can I know the criteria applied using vba?

  9. #9
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Clear contents for filtered range only

    Quote Originally Posted by YasserKhalil View Post
    Suppose I have the range filtered but I don't know the criteria applied to that range .. How can I know the criteria applied using vba?
    I don't know the answer of the question.
    But if you want to clear the contens of the hidden cells (in filtered range) without knowing the filter criteria, you can do something like this:
    Sub b1138935()
    Dim r As Range, q As Range
    Set q = Range("A2:A24")
    Set r = Range("A2:A24").SpecialCells(xlCellTypeVisible)
    q.AutoFilter
    r.EntireRow.Hidden = True
    q.SpecialCells(xlCellTypeVisible).ClearContents
    q.EntireRow.Hidden = False
    q.AutoFilter Field:=1, Criteria1:="<>"
    End Sub

  10. #10
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Clear contents for filtered range only

    Quote Originally Posted by YasserKhalil View Post
    I have the range filtered but I don't know the criteria applied to that range .. How can I know the criteria applied using vba?
    Check the below links which will give you some idea about how to get it in VBA.

    http://www.ozgrid.com/VBA/autofilter-criteria.htm

    http://datapigtechnologies.com/blog/...in-status-bar/

    http://www.exceltip.com/general-topi...oft-excel.html

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Clear contents for filtered range only

    Thank you very much Mr. Akuini for this great code .. It is wonderful and very easy at the same time
    Thanks a lot Mr. Sixthsense for these useful and great links that gave me ideas about the issue
    Best Regards

  12. #12
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Clear contents for filtered range only

    Quote Originally Posted by YasserKhalil View Post
    Thank you very much Mr. Akuini for this great code .. It is wonderful and very easy at the same time
    Thanks a lot Mr. Sixthsense for these useful and great links that gave me ideas about the issue
    Best Regards
    You're welcome & thanks for the feedback

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Clear the contents of Unprotected cells - EXCEPT a certain range
    By ronkeakano in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-13-2015, 01:40 PM
  2. Clear contents in dynamic range
    By DCRAIG3389 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2015, 10:49 AM
  3. [SOLVED] Clear Contents of range, offset and repeat
    By mattmickle in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2014, 05:07 PM
  4. [SOLVED] If each cell in range equals 0 then clear contents of range and move to the next row
    By dagardner in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-19-2014, 08:44 PM
  5. [SOLVED] Clear Range Contents
    By kenadams378 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-23-2013, 07:06 AM
  6. [SOLVED] clear contents of range
    By KATIEexcel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-14-2012, 08:11 AM
  7. Clear contents of cells in a range, LEN=0
    By jenny_journalist in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-29-2009, 11:37 AM

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