removing words only from the left..

nafri

New Member
Joined
Apr 13, 2008
Messages
13
Hi

Got a small issue.

I need to remove specific words from a coloumn but only if the characters of the first word in the coloumn matches them.

In the folowing example i only wish to remove the first blah ,bla,pan,loc,sugar not the second if it exist.Not all words have repeaters.The important thing is i only wish to remove the word if the characters match the first word on the left .

A1
blah23eweew blah ---- this becomes 23eweew blah
bla23dddd bla --- this becomes 23dddd bla
pandfgsswer pan --- this becomes dfgsswer pan
loc2341 loc --- this becomes 2341 loc
sugar23wewew sugar --- this becomes 23wewew sugar

kind regards

nafri
 
Try this UDF


Function Tidy(s As String) As String
Dim arr
Dim p As String
Dim i As Long

Static RegEx As Object

If RegEx Is Nothing Then
Set RegEx = CreateObject("VBScript.RegExp")
arr = Array("toslcdtv", "samlcdtv", "sanlcd", "shalcdtv", "shalcd", "PHILCD", "CELLO", "PHITFT", _
"HANNSG", "IIY", "NECTFT", "NECLCD", "BTE", "lgp", "ppd", "samp", "pand", _
"sam", "peer", "lox", "sony", "chief", "bosch", "onk", "phi", "hk", "pan", "lg")
For i = LBound(arr) To UBound(arr)
p = p & "|" & arr(i)
Next i
p = "^(" & Replace(p, "|", "", 1, 1) & ")"
With RegEx
.Global = True
.IgnoreCase = True
.Pattern = p
End With
End If
Tidy = RegEx.Replace(s, "")
End Function




Excel Workbook
FG
1toslcdtvaa
2samlcdtvzzzzzzzz
3sanlcd
4abc shalcdtvabc shalcdtv
5hhhhshalcdchhhhshalcdc
Sheet1


Many thanks .. to all.. this solution works for me.

cheers
nafri
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Thanks for the feedback.

If your 'Removal List' is in the sheet as per Ron's example in post #9, then rather than repeat all the entries in the UDF (and add/remove/change if the list changes) you could modify the UDF like this. My 'Removal List' starts in K2.

<font face=Courier New><br><SPAN style="color:#00007F">Function</SPAN> Tidy(s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> p <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><br>    <SPAN style="color:#00007F">Static</SPAN> RegEx <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br>    <br>    Application.Volatile<br>    <SPAN style="color:#00007F">If</SPAN> RegEx <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> RegEx = CreateObject("VBScript.RegExp")<br>        <SPAN style="color:#00007F">With</SPAN> RegEx<br>            .Global = <SPAN style="color:#00007F">True</SPAN><br>            .IgnoreCase = <SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    p = "^(" & Join(Application.Transpose(Range("K2", _<br>        Range("K" & Rows.Count).End(xlUp))), "|") & ")"<br>    RegEx.Pattern = p<br>    Tidy = RegEx.Replace(s, "")<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><br></FONT>


Excel Workbook
FGHIJK
1toslcdtvaaRemoval List
2samlcdtvzzzzzzzztoslcdtv
3sanlcdsamlcdtv
4abc shalcdtvabc shalcdtvsanlcd
5hhhhshalcdchhhhshalcdcshalcdtv
6shalcd
7PHILCD
8CELLO
9PHITFT
10HANNSG
11IIY
12NECTFT
13NECLCD
14BTE
15lgp
16ppd
17samp
18pand
19sam
20peer
21lox
22sony
23chief
24bosch
25onk
26phi
27hk
28pan
29lg
30
Sheet1
 
Upvote 0
There is a further point/question. In the list that you gave, I note that you have "samlcdtv" and "sam". So if a cell contains "samlcdtvzzzz", how do we know what result you want returned? Is it "zzzz" (removing "samlcdtv") or is it "lcdtvzzzz" (removing "sam" only)?

With my UDF, the order of the items in the list determines the outcome. Refer to the following two screen shots where F1 is the same in each case but the green items in column K are reversed.

So, depending on your circumstances, further code modification may be required, or you may decide to carefully choose the order you place the items in the column (or in the code array if you are sticking with my earlier code).

Excel Workbook
FGHIJK
1samlcdtvzzzzzzzzRemoval List
2toslcdtv
3samlcdtv
4sanlcd
5pand
6sam
7peer
8
Sheet2



Excel Workbook
FGHIJK
1samlcdtvzzzzlcdtvzzzzRemoval List
2toslcdtv
3sam
4sanlcd
5pand
6samlcdtv
7peer
8
Sheet2
 
Upvote 0

Forum statistics

Threads
1,225,149
Messages
6,183,193
Members
453,151
Latest member
Lizamaison

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