+ Reply to Thread
Results 1 to 8 of 8

Remove Rows and Columns if text not found.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2023
    Location
    England
    MS-Off Ver
    365
    Posts
    2

    Question Remove Rows and Columns if text not found.

    Hi All, hoping someone can help as I've been faffing for hours with this.

    I have a large spreadsheet (60x2500) and It comprises of cells that are pretty binary. Either Pass or fail.
    I want to remove any columns and rows that don't have the 'Fail' text in them.

    Is this possible with VBA? Can anyone be so kind and give me a hand?

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Remove Rows and Columns if text not found.

    It would be easier to answer if you posted a sample file. Assuming you're cycling through all cells in your range you can use the wildcard character to test for "Fail" .. or, if the cell only contains the word, just search for it and delete the row. e.g.
    Private Sub DeleteFails()
    Dim cel As Range, rng As Range
    
        Set rng = Sheet(1).Range("A1:D100")     'set to whatever your range is.
        On Error Resume Next                    'for when the text is not found.
        Err.Clear
        Set cel = rng.Find(what:="Fail", LookIn:=xlValues, lookat:=xlWhole)
        While Err.Number = 0
            cel.EntireRow.Delete shift:=xlUp
            Set cel = rng.FindNext(cel)
        Wend
        On Error GoTo 0
        Err.Clear
        
    End Sub
    Hope that helps.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Remove Rows and Columns if text not found.

    Sorry - I misread your post .. correction coming.

  4. #4
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Remove Rows and Columns if text not found.

    Here ya go:
    Private Sub DeleteFails()
    Dim i As Long
    Dim booFound As Boolean
    Dim cel As Range, rng As Range
    
        Set rng = ThisWorkbook.Worksheets(1).Range("A1:D100")     'set to whatever your range is.
        On Error Resume Next                    'for when the text is not found.
        Err.Clear
        
        'check columns first:
        For i = rng.Columns.Count To 1 Step -1
            Set cel = rng.Columns(i).Find(what:="Fail", LookIn:=xlValues, lookat:=xlWhole)
            If cel Is Nothing Then rng.Columns(i).Delete
        Next i
        
        'then rows:
        For i = rng.Rows.Count To 1 Step -1
            Set cel = rng.Rows(i).Find(what:="Fail", LookIn:=xlValues, lookat:=xlWhole)
            If cel Is Nothing Then rng.Rows(i).Delete
        Next i
        
        On Error GoTo 0
        Err.Clear
        
    End Sub
    If you want to search for anything containing "fail" but not necessarily the whole word - e.g. failure, or failed etc - use this in the search:
    Set cel = rng.Columns(i).Find(what:="Fail", LookIn:=xlValues, lookat:=xlPart)
    Hope that helps.
    Last edited by MatrixMan; 08-17-2023 at 11:22 AM.

  5. #5
    Registered User
    Join Date
    08-17-2023
    Location
    England
    MS-Off Ver
    365
    Posts
    2

    Re: Remove Rows and Columns if text not found.

    example.xlsx

    Thank you so much for your help. I've attached an example of the file, hopefully that helps.
    I tried applying the script and it did a great job, however the columns lost their context. Is it possible when deleting a column/row for the following entire column to move along?
    Does that make sense?

    Thanks again

  6. #6
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Remove Rows and Columns if text not found.

    Quote Originally Posted by photom View Post
    Attachment 839910

    Thank you so much for your help. I've attached an example of the file, hopefully that helps.
    I tried applying the script and it did a great job, however the columns lost their context. Is it possible when deleting a column/row for the following entire column to move along?
    Does that make sense?

    Thanks again
    Who are you replying to - me or Glenn? In my example the columns shift along to the left and the rows shift up, however, since you asked to delete the entire column and row, you would lose the headers in row 1 (A1, A2 etc) and the names in the first column. To avoid this, set your rng to B2:AQ27 and use this instead of Delete:
    If cel Is Nothing Then rng.Columns(i).ClearContents
    ... and same for rows. See attached workbook.
    Attached Files Attached Files

  7. #7
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,662

    Re: Remove Rows and Columns if text not found.

    This might seem like a silly question, but your explanation was ambiguous.

    What are your expected result in this file?

    Is it just the red cells in the file, since the rows and columns of the orange cells contain no fails



    or something else?
    Attached Files Attached Files
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Remove Rows and Columns if text not found.


    Quote Originally Posted by photom View Post
    Is this possible with VBA? Can anyone be so kind and give me a hand?
    Hi,

    yes with exactly the same way any Excel user operating manually in less than ten seconds !
    Just using a filter, under VBA reproducing this way should need less than ten codelines,
    instant result, looping is useless, easy to start yourself just activating the Macro Recorder …

+ 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. Remove rows in a single cell when matching text found in the row
    By Rem0ram in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 01-17-2019, 07:19 PM
  2. Remove rows when a column is found to contain multiple criteria
    By cmorten82 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-08-2016, 05:59 PM
  3. [SOLVED] Search for the text in cell A1 in a list and remove the text if found in the list
    By kak0 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-24-2014, 11:01 PM
  4. Replies: 1
    Last Post: 12-11-2013, 09:09 PM
  5. Compare 2 columns and remove duplicates in both columns when found
    By 1dtms in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-11-2013, 02:19 AM
  6. excel macro to remove specific columns and rows + remove duplicate
    By garrywelson in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-17-2013, 12:03 PM
  7. [SOLVED] Remove all rows after Value is found in column
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-03-2011, 04:28 PM

Tags for this Thread

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