+ Reply to Thread
Results 1 to 8 of 8

Remove Rows and Columns if text not found.

Hybrid View

  1. #1
    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.
    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.

  2. #2
    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

  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.

    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

+ 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