+ Reply to Thread
Results 1 to 7 of 7

Find and replace XYZ but not when WXYZ

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Find and replace XYZ but not when WXYZ

    Hi all,

    How do I delete an entire row based on cells in Column A that contain "Billy" without the speech marks.

    Issue is that I may have other cells in the column which have the same name, i.e:
    1. Billy Davis
    2. Billy F
    3. Billy

    In the above I would just like the row of the last example to be deleted.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Find and replace XYZ but not when WXYZ

    dim rng as range 
    set rng = Columns("A:").Find(What:="Billy", After:=Cells(rows.count, "A"), LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            True, SearchFormat:=False)
    if not rng is nothing then
      rng.entirerow delete
    endif
    if needed (probably yes) then do it in a loop (until rng is nothing)

  3. #3
    Forum Contributor
    Join Date
    12-31-2012
    Location
    Jhang, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Find and replace XYZ but not when WXYZ

    Sub DelRows()
        Dim r As Range
        For Each r In Range("A1:A1000")
            If r.Text = "Billy" Then r.EntireRow.Delete
        Next
    End Sub

  4. #4
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Find and replace XYZ but not when WXYZ

    Hi,

    @Kaper
    I receive a Compile error: Invalid use of property on this line:

    rng.EntireRow Delete
    @hafizimran

    I couldn't get yours to work I'm afraid for the whole column. It kind of only applied to the last few cells and I had to keep on re-running it to work on them all.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Find and replace XYZ but not when WXYZ

    Hi Plumby,
    of course there is missing dot between entirerow and delete
    try full macro:
    Sub test()
    Dim rng As Range
    Do
    Set rng = Columns("A:A").Find(What:="Billy", After:=Cells(Rows.Count, "A"), LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            True, SearchFormat:=False)
    If Not rng Is Nothing Then
      rng.EntireRow.Delete
    End If
    Loop Until rng Is Nothing
    End Sub

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Find and replace XYZ but not when WXYZ

    Hi Plumby,
    maybe so
    Sub ertert()
    With Range("A1", Cells(Rows.Count, 1).End(xlUp))
        .Replace "*Billy", True, xlWhole
    '    .Replace "Billy", True, xlWhole
        .SpecialCells(2, 4).EntireRow.Delete
    End With
    End Sub

  7. #7
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Find and replace XYZ but not when WXYZ

    Thanks!! Both work.

+ 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. [SOLVED] find and replace to not replace characters found as wildcards
    By sabutler4 in forum Excel General
    Replies: 4
    Last Post: 07-03-2013, 06:48 PM
  2. find in excel replace in word: find/replace text in text boxes and headers
    By dean.rogers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2012, 12:40 PM
  3. Multiple Find and Replace to replace a list of strings
    By WalterP34 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-11-2011, 07:41 PM
  4. Find/Replace Macro Using Replace Table
    By LampCommandr in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-24-2011, 11:00 AM
  5. [SOLVED] find and replace - replace data in rows to separated by commas
    By msdker in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-14-2006, 08:10 PM

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