+ Reply to Thread
Results 1 to 18 of 18

Select each row one by one and check for a substring in each cell of it

Hybrid View

  1. #1
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,568

    Re: Select each row one by one and check for a substring in each cell of it

    Hi Abhushan86,

    Welcome to the forum!!

    As well as jraj1106's clever solution here's my attempt (which may also help anyone who is unable to download from the Internet):

    Option Explicit
    Sub Macro1()
    
        'http://www.excelforum.com/excel-programming/827684-select-each-row-one-by-one-and-check-for-a-substring-in-each-cell-of-it.html
        
        Dim lngLastRow As Long, _
            lngMyRow As Long
        Dim strMyText As String
        Dim strMyCols As String, _
            strMyColFrom As String, _
            strMyColTo As String
        Dim varMyCol As Variant
        
        Application.ScreenUpdating = False
        
        strMyCols = "D:E" 'Columns to be checked for 'strMyText'. Change to suit.
        strMyText = "Good morning" 'Text to be search in 'strMyCols'. Change to suit.
        
        lngLastRow = Range(strMyCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        For Each varMyCol In Split(strMyCols, ":")
            If strMyColFrom = "" Then
                strMyColFrom = varMyCol
            Else
                strMyColTo = varMyCol
            End If
        Next varMyCol
        
        For lngMyRow = lngLastRow To 2 Step -1
            If Evaluate("COUNTIF(" & strMyColFrom & lngMyRow & ":" & strMyColTo & lngMyRow & ",""*" & strMyText & "*"")") > 0 Then
                Rows(lngMyRow).EntireRow.Delete
            End If
        Next lngMyRow
        
        Application.ScreenUpdating = True
        
        MsgBox "Any applicable rows have now been deleted."
        
    End Sub
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  2. #2
    Registered User
    Join Date
    04-27-2012
    Location
    New Delhi
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Select each row one by one and check for a substring in each cell of it

    Quote Originally Posted by Trebor76 View Post
    Hi Abhushan86,

    Welcome to the forum!!

    As well as jraj1106's clever solution here's my attempt (which may also help anyone who is unable to download from the Internet):

    Option Explicit
    Sub Macro1()
    
        'http://www.excelforum.com/excel-programming/827684-select-each-row-one-by-one-and-check-for-a-substring-in-each-cell-of-it.html
        
        Dim lngLastRow As Long, _
            lngMyRow As Long
        Dim strMyText As String
        Dim strMyCols As String, _
            strMyColFrom As String, _
            strMyColTo As String
        Dim varMyCol As Variant
        
        Application.ScreenUpdating = False
        
        strMyCols = "D:E" 'Columns to be checked for 'strMyText'. Change to suit.
        strMyText = "Good morning" 'Text to be search in 'strMyCols'. Change to suit.
        
        lngLastRow = Range(strMyCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        For Each varMyCol In Split(strMyCols, ":")
            If strMyColFrom = "" Then
                strMyColFrom = varMyCol
            Else
                strMyColTo = varMyCol
            End If
        Next varMyCol
        
        For lngMyRow = lngLastRow To 2 Step -1
            If Evaluate("COUNTIF(" & strMyColFrom & lngMyRow & ":" & strMyColTo & lngMyRow & ",""*" & strMyText & "*"")") > 0 Then
                Rows(lngMyRow).EntireRow.Delete
            End If
        Next lngMyRow
        
        Application.ScreenUpdating = True
        
        MsgBox "Any applicable rows have now been deleted."
        
    End Sub
    HTH

    Robert
    Thanks for the help Robert. \m/

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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