Consulting

Results 1 to 13 of 13

Thread: Delete text line with specific word from multiline cell

  1. #1

    Delete text line with specific word from multiline cell

    Hi folks,

    I have excel file with a lot of cells with multiline text. Is there a way to delete line that contains a certain word?

    e.g.
    I want to find all lines that contains PH and remove that lines

    this:
    Colour protection from fading.
    PH 4.5 technology.
    shampoo + acidic sealer.

    to became:
    Colour protection from fading.
    Shampoo + acidic sealer.

    Thank you for your time.
    Last edited by kurdjup; 10-28-2019 at 10:10 AM.

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Hi and welcome to the forum.

    Is it always PH or sometimes ph, Ph or pH?
    Is it always at the start of the line or, for example 'The PH 4.5 technology was used.'
    Are the lines always in the same column?
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Make a back-up copy of your sheet before you run any code which deletes stuff (like this code!)

    This will delete anything starting with ph in column A.

    Sub DelPH()
        Dim i As Long, txt As String
        For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
            txt = Left(Cells(i, 1), 2)
            If txt = "PH" Or txt = "pH" Or txt = "ph" Or txt = "Ph" Then
                Rows(i).Delete
            End If
        Next
    End Sub
    Semper in excretia sumus; solum profundum variat.

  4. #4
    Hi paulked.

    It is always PH and it is always in the same column.
    Is not always at the start of the line, sometimes it's in the middle of a sentence.

  5. #5
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Sub DelPH()
        Dim i As Long
        For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
            If InStr(Cells(i, 1), "PH") Then
                Rows(i).Delete
            End If
        Next
    End Sub
    Semper in excretia sumus; solum profundum variat.

  6. #6
    Thanks paulked, it works but script delete entire row.
    Is it possible to delete only one line of text (which contain PH) inside of cell, not entire row?

  7. #7
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Sorry, I misunderstood you.

    It is possible but I'm off out shortly.

    Can you post some typical data or the file and I'll have a look when I get back.

    Thanks
    Semper in excretia sumus; solum profundum variat.

  8. #8
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Sub DelPH()
        Dim i As Long, j&, txtOld() As String, txtNew$
        For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
            txtNew = ""
            If InStr(Cells(i, 1), "PH") Then
                txtOld = Split(Cells(i, 1), Chr(10))
                For j = LBound(txtOld) To UBound(txtOld)
                    If InStr(txtOld(j), "PH") = 0 Then
                        txtNew = txtNew & txtOld(j) & Chr(10)
                    End If
                Next
                Cells(i, 1) = txtNew
            End If
        Next
    End Sub
    Out the door now, not ignoring you!!
    Semper in excretia sumus; solum profundum variat.

  9. #9
    It works now!

    paulked thank you very much for your time!
    Last edited by kurdjup; 10-28-2019 at 11:59 AM.

  10. #10
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    You're welcome

    Just to tidy up, add the line in red. It will delete the last Chr(10) from the string so you don't have an empty line at the bottom of the cell.

    Sub DelPH()
        Dim i As Long, j As Long, txtOld() As String, txtNew As String
        For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
            txtNew = ""
            If InStr(Cells(i, 1), "PH") Then
                txtOld = Split(Cells(i, 1), Chr(10))
                For j = LBound(txtOld) To UBound(txtOld)
                    If InStr(txtOld(j), "PH") = 0 Then
                        txtNew = txtNew & txtOld(j) & Chr(10)
                    End If
                Next
                txtNew = Left(txtNew, (Len(txtNew) - 1))
                Cells(i, 1) = txtNew
            End If
        Next
    End Sub
    Semper in excretia sumus; solum profundum variat.

  11. #11
    Thanks again
    I managed to solve that minor "problem" but certainly a more elegant solution is one script.

  12. #12
    VBAX Newbie
    Joined
    Sep 2021
    Posts
    1
    Location
    Quote Originally Posted by kurdjup View Post
    Thanks again
    I managed to solve that minor "problem" but certainly a more elegant solution is one script.

    Hi, I know that this is quite an old thread, I need this now. Exactly the same BUT my requirement is that, entire row WILL NOT BE DELETED. Example in Column G, columns have this:

    HERR-101, HERG-789, HERT-789

    Only the "HERR-101" will be deleted and will retain "HERG-789", "HERT-789". The number after "HERR" is also changing so I am thinking of just use "HERR-".

    Please help. Thank you in advance!

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Please start a new thread.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •