+ Reply to Thread
Results 1 to 4 of 4

Remove all rows EXCEPT those with specific string in specific column

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2017
    Location
    Toronto
    MS-Off Ver
    2016
    Posts
    81

    Question Remove all rows EXCEPT those with specific string in specific column

    Hello all,

    As the title says, I'm trying to modify a macro that was originally used to remove all rows containing a set of strings in specific columns, to now remove all rows except those with a specific string in a specific column.

    I have the following, which doesn't give any error but does not seem to do anything:

    Sub RemoveAllExcept()
        Dim p, r As Range
        Application.ScreenUpdating = False
        p = "sup":
        With Sheets("sheet1") 'Applies to this Sheet Name
            If .FilterMode Then .ShowAllData
            Set r = .[f1:f2]
                    
            r(2).Formula = "=(a2<>" & p & " )" 'A2 i.e. this must be the column which contains the "except" text
            With .Cells(1).CurrentRegion
                .AdvancedFilter 1, r
                .Offset(1).EntireRow.Delete
            End With
            If .FilterMode Then .ShowAllData
            r.Clear
            .ListObjects(1).ShowAutoFilter = True
        End With
        Application.ScreenUpdating = True
        
        MsgBox ("Done")
    End Sub
    I have attached a sample workbook with the original sheet ("Sheet1"), and a results sheet, along with the macro. Simply click on the button to run the macro, though as I said earlier, it won't do anything other than show a message.

    Thanks for any help.
    Attached Files Attached Files
    Last edited by noname91; 09-05-2023 at 01:08 PM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Remove all rows EXCEPT those with specific string in specific column

    Hi there,

    Take a look at the attached version of your workbook and see if it does what you need. It uses the following code:

    
    
    
    Option Explicit
    
    
    Sub RemoveAllExcept()
    
        Const sTEXT_TO_KEEP As String = "sup"
        Const sTYPE_HEADER  As String = "Type"
        Const sSHEET_NAME   As String = "Sheet1"
        Const sTABLE_NAME   As String = "tblData"
    
        Dim rTypeColumn     As Range
        Dim iNoOfRows       As Integer
        Dim wksTarget       As Worksheet
        Dim lobData         As ListObject
        Dim iRowNo          As Integer
    
        Set wksTarget = ThisWorkbook.Worksheets(sSHEET_NAME)
        Set lobData = wksTarget.ListObjects(sTABLE_NAME)
    
        Set rTypeColumn = lobData.ListColumns(sTYPE_HEADER).DataBodyRange
    
        iNoOfRows = rTypeColumn.Rows.Count
    
        For iRowNo = iNoOfRows To 1 Step -1
    
            If rTypeColumn.Cells(iRowNo, 1).Value <> sTEXT_TO_KEEP Then
                lobData.DataBodyRange.Rows(iRowNo).Delete
            End If
    
        Next iRowNo
    
    End Sub
    The highlighted values may be altered to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    05-08-2017
    Location
    Toronto
    MS-Off Ver
    2016
    Posts
    81

    Re: Remove all rows EXCEPT those with specific string in specific column

    Hi Greg M.

    Works perfectly! Even allows for easy adjustment to allow for more than one exception strings.

    Thanks again!

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Remove all rows EXCEPT those with specific string in specific column

    Hi again,

    Many thanks for your very prompt feedback and also for the Reputation increase - much appreciated!

    You're welcome - glad I was able to help.

    Best regards,

    Greg M

+ 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. Replies: 2
    Last Post: 05-06-2022, 02:12 PM
  2. Macro to remove rows that don't contain a specific string in column B
    By tisahardknocklife in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-16-2018, 08:21 PM
  3. Replies: 16
    Last Post: 04-10-2016, 07:51 AM
  4. Delete Rows with specific text in specific column
    By love2waltz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-12-2015, 10:16 AM
  5. [SOLVED] Copy rows with specific text in specific column into specific sheet
    By Valemaar in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 08-22-2014, 03:23 PM
  6. Replies: 0
    Last Post: 10-23-2012, 04:59 PM
  7. Modify to Run On Specific Column and Remove trailing digits in string
    By pmterp in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-12-2012, 07:47 PM

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