Remove specific text within a string

Status
Not open for further replies.

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
I have the range A1:A100
I want to remove specific text within the cell value from the exclamation mark to the end
Note that the cell value may contain more than exclamation mark .. I mean the last one
Example:
No way of escaping fate!It's your destiny!Is it right?
The string "!Is it right?" have to be removed
I want to do that by code
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I have the range A1:A100
I want to remove specific text within the cell value from the exclamation mark to the end
Note that the cell value may contain more than exclamation mark .. I mean the last one
Example:
No way of escaping fate!It's your destiny!Is it right?
The string "!Is it right?" have to be removed
I want to do that by code
Something like...

B1, copy down:
Code:
=IF(ISNUMBER(FIND("!",A1)),
   LEFT(A1,FIND("#",SUBSTITUTE(A1,"!","#",
    LEN(A1)-LEN(SUBSTITUTE(A1,"!",""))))-1),A1)
 
Upvote 0
Hi Yasser السلام عليكم


something like this maybe :

Code:
Function EndToInstrRev( _
ByVal StringCheck As String, _
ByVal StringMatch As String _
) As String

    If InStrRev(StringCheck, StringMatch) > 0 Then
        EndToInstrRev = Right(StringCheck, Len(StringCheck) - _
        InStrRev(StringCheck, StringMatch))
    End If

End Function

Sub Test()

    Dim s As String
    
    s = "No way of escaping fate!It's your destiny!Is it right?"
    
    MsgBox EndToInstrRev(s, "!")

End Sub
 
Upvote 0
Sorry Yasser. I misunderstood your question- I think you mean this :

Code:
Function StartToInstrRev( _
ByVal StringCheck As String, _
ByVal StringMatch As String _
) As String

    If InStrRev(StringCheck, StringMatch) > 0 Then
        StartToInstrRev = Left(StringCheck, InStrRev(StringCheck, StringMatch) - 1)
    End If

End Function

Sub Test()

    Dim s As String
    
    s = "No way of escaping fate!It's your destiny!Is it right?"
    
    MsgBox StartToInstrRev(s, "!")

End Sub
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,565
Messages
6,179,549
Members
452,927
Latest member
rows and columns

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top