Sunday, July 1, 2012

MS Excel: Get rid of rows that contain a particular string

Hey guys,

This is how it is done. We make use of macros to achieve this.

  1. If the developer tab is not visible make it visible as follows: (MS Excel 2007)
    1. Click the office button
    2. Then, click the Excel Options button
    3. Choose "Popular" tab
    4. In "Top option for working with Excel", tick the "Show developer tab in the Ribbon"

  2. Now go to the developer tab and click on the visual basic button.

  3. Double click on the sheet1(sheet1)...or the sheet you want the macro to run

  4. Now enter this code:

  5. Sub DoIt()
        Dim Sentences
        Dim i As Long
        Dim iWordPos As Integer
        
        Sentences = Range("A1", Range("A65536").End(xlUp))
        lRow = 0
        For i = Range("A65536").End(xlUp).Row To 1 Step -1
            iWordPos = InStr(LCase(Sentences(i, 1)), LCase("MyString"))
            If iWordPos > 0 Then
                Range("A" & i).EntireRow.Delete shift:=xlShiftUp
            End If
        Next i
    End Sub

    'Comment : Range("A65536").End(xlUp) is the final cell
  6. Replace MyString with the string you want to search

No comments:

Post a Comment