+ Reply to Thread
Results 1 to 4 of 4

Deleting rows based upon partial cell text

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2003
    Location
    Decatur, Alabama
    Posts
    94

    Deleting rows based upon partial cell text

    Dear Friends,

    I have read many posts to this forum where a question like my own is posted, except those questions are based upon exact cell values/contents, but not partial contents. So .. I will ask my question below.

    I would like to delete all rows (NOT the header row) where cells in column H DON'T start with the word "Name:" The cell may read "Name: John Doe" or it may read "Name: Sam Smith" or even "Betty." Of these, the row with "Betty" would be deleted, the others would remain.

    Thanks for your help,
    Mike
    Learn to Serve Others. Kindness is far better than the alternative.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Mike

    Try this
    Sub ccc()
      For i = Cells(Rows.Count, "H").End(xlUp).Row To 2 Step -1
        If Left(Cells(i, "H"), 5) <> "Name:" Then Cells(i, "A").EntireRow.Delete
      Next i
    End Sub
    rylo

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello mjwillyone,

    Here is another version that matches "Name:" anywhere in the cell and isn't case sensitive. More code than Rylo's, but a bit more robust if you need it. For all the code, it is still very quick.
    Sub DeleteNonMatchingRows()
    
      Dim Col As Variant
      Dim DelRng As Range
      Dim LastRow As Long
      Dim FirstAddx As String
      Dim SrchCell As Range
      Dim SrchRng As Range
      Dim SrchValue As Variant
      Dim StartRow As Long
      
        Col = "H"
        StartRow = 2
        SrchValue = "Name:"
        LastRow = Cells(Rows.Count, "H").End(xlUp).Row
        LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
            
        Set SrchRng = Range(Cells(StartRow, Col), Cells(LastRow, Col))
    
          Set SrchCell = SrchRng.Find(What:=SrchValue, After:=SrchRng.Cells(1, 1), _
                                      LookIn:=xlValues, LookAt:=xlPart, _
                                      SearchOrder:=xlRows, _
                                      SearchDirection:=xlNext, _
                                      MatchCase:=False)
            If Not SrchCell Is Nothing Then
              FirstAddx = SrchCell.Address
              Set DelRng = SrchCell
                Do
                  Set DelRng = Union(DelRng, SrchCell)
                  Set SrchCell = SrchRng.FindNext(SrchCell)
                Loop While Not SrchCell Is Nothing And SrchCell.Address <> FirstAddx
            End If
         
          For R = SrchRng.Rows.Count + StartRow - 1 To StartRow Step -1
            If Intersect(SrchRng.Cells(R, 1), DelRng) Is Nothing Then
              SrchRng.Cells(R, 1).EntireRow.Delete Shift:=xlShiftUp
            End If
          Next R
        
    End Sub
    Sincerely,
    Leith Ross

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588
    try
    No loop
    Sub test()
    Columns(1).Insert
    With Range("i2", Range("i" & Rows.Count).End(xlUp)).Offset(,-8)
        .Formula = "=if(left(i2,5)<>""Name"",1,"""")"
        On Error Resume Next
        .SpecialCells(-4123,1).EntireRow.Delete
    End With
    Columns(1).Delete
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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