+ Reply to Thread
Results 1 to 13 of 13

Delete Row with string containing special character

Hybrid View

  1. #1
    Registered User
    Join Date
    09-05-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2013
    Posts
    71

    Delete Row with string containing special character

    I am trying to delete rows that contain text with a special character in column O. The text I am searching for contains PO# and will look something like this PO#B7605887-1, so I need it to be PO#* so it catches the text if it contains anything after the special character.

    The code I created below is not working. What am I doing wrong?

    Sub Delete_POnums()
        Dim k As Long 'PO#
               On Error GoTo 0
    
            With ThisWorkbook.Sheets("Call Data").Range("O:O")
                    For k = .Cells(.Rows.Count, 1).End(xlUp).Row To 1 Step -1
                            With .Cells(k, 1)
                        If CStr(.Value) Like "PO" & Chr(35) & "*" Then .EntireRow.Delete Shift:=xlUp
                    End With
                Next k
            End With
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Delete Row with string containing special character

    try this change
    For k = .Cells(.Rows.Count, "O").End(xlUp).Row To 1 Step -1
    If solved remember to mark Thread as solved

  3. #3
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete Row with string containing special character

    Do any values contain the letters "PO" without containing the symbol #?

    maybe
    Sub Delete_POnums()
    Dim k As Long 'PO#
    
    With ThisWorkbook.Sheets("Call Data")
        For k = .Cells(.Rows.Count, 15).End(xlUp).Row To 1 Step -1
            If .Cells(k, 15).Value Like "PO*" Then .Cells(k, 1).EntireRow.Delete Shift:=xlUp
        Next k
    End With
    End Sub
    Last edited by Solus Rankin; 02-24-2014 at 02:31 PM.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  4. #4
    Registered User
    Join Date
    09-05-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2013
    Posts
    71

    Re: Delete Row with string containing special character

    Solus, if I add the * after "PO", then it would delete also delete rows with words that start with a PO, like possible or potential, etc. I need it to delete rows with the phrase "PO#".

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete Row with string containing special character

    Hi, John,

    your code
    Range("O" & Rows.count).End(3)(1).Row
    isnīt qualfied for Sheets("Call Data") but should refer to the ActiveSheet instead.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete Row with string containing special character

    Quote Originally Posted by HaHoBe View Post
    Hi, John,

    your code
    Range("O" & Rows.count).End(3)(1).Row
    isnīt qualfied for Sheets("Call Data") but should refer to the ActiveSheet instead.

    Ciao,
    Holger

    "Call Data" is the activesheet? Isn't it?

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete Row with string containing special character

    Hi, John,

    I donīt know to be honest. But if I see a code like
    With ThisWorkbook.Sheets("Call Data").Range("O:O")
    Iīd go the way to use
    Sub DoodlesMama()
    With Sheets("Call Data")
      .Range("O1:O" & .Range("O" & Rows.Count).End(3)(1).Row).AutoFilter 1, "=PO#*"
      .Range("O1:O" & .Range("O" & Rows.Count).End(3)(1).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      .AutoFilterMode = False
    End With
    End Sub
    to avoid any possible other activesheet or
    Sub DoodlesMama2()
    With Sheets("Call Data")
      .Range("O1", .Range("O" & Rows.Count).End(3)(1)).AutoFilter 1, "=PO#*"
      .Range("O1", .Range("O" & Rows.Count).End(3)(1)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      .AutoFilterMode = False
    End With
    End Sub
    Ciao,
    Holger

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete Row with string containing special character

    Another:

    Sub DoodlesMama()
    Sheets("Call Data").Range("O1:O" & Range("O" & Rows.count).End(3)(1).Row).AutoFilter 1, "=*PO#*"
    Sheets("Call Data").Range("O1:O" & Range("O" & Rows.count).End(3)(1).Row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Sheets("Call Data").AutoFilterMode = False
    End Sub
    Last edited by JOHN H. DAVIS; 02-24-2014 at 03:42 PM.

  9. #9
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete Row with string containing special character

    Sorry. This should fix that problem:
    Sub Delete_POnums()
    Dim k As Long 'PO#
    
    With ThisWorkbook.Sheets("Call Data")
        For k = .Cells(.Rows.Count, 15).End(xlUp).Row To 1 Step -1
            If .Cells(k, 15).Value Like "PO?#*" Then .Cells(k, 1).EntireRow.Delete Shift:=xlUp
        Next k
    End With
    End Sub

  10. #10
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: Delete Row with string containing special character

    Try this...

    Sub tester()
    
    Dim ws As Worksheet
    Set ws = Sheets("Call Data")
    
    Dim r1 As Long  'Row to delete...
    Dim s1 As Long  'Sum of deletions...
    Dim i As Long
    
    s1 = 0
    
    Do While 1
        On Error GoTo ErrorHandler
            r1 = ws.Columns("O:O").Find(What:="PO#", LookIn:=xlValues).Row
        If r1 > 0 Then
            s1 = s1 + 1
        End If
        ws.Rows(r1 & ":" & r1).Delete
        i = i + 1
    Loop
    
    
    ErrorHandler:
        MsgBox s1 & " rows removed!"
        Exit Sub
    
    End Sub
    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. Replies: 2
    Last Post: 12-02-2013, 03:29 PM
  2. [SOLVED] Finding special character in a string
    By sathishkm in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-01-2013, 06:10 AM
  3. [SOLVED] extract string after nth string (special character)
    By itselflearn in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-24-2013, 03:18 PM
  4. [SOLVED] Delete row that contains special character *
    By canada123 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-17-2013, 12:25 PM
  5. remove special character at end of string
    By captain bob in forum Excel General
    Replies: 0
    Last Post: 08-03-2006, 09:59 AM

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