UDF to remove words from a text string

stodge

New Member
Joined
Jan 19, 2010
Messages
4
I am trying to write a UDF to remove certain words from an imported text string.

For example, an imported text string might be air-conditioning systems or aircon system

I want to be able to remove the "-" "systems" and "system". I also want to easily add other words to exclusion list in due course.

I can do this easily using a macro but would like to create a UDF to achieve the same result.

Thanks in advance for your help.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
There shouldn't be a huge difference between a macro and a UDF in terms of code, so where are you getting stuck?
 
Upvote 0
Sorry, I am new to UDFs.

The macro I am using is:

Sub Remove_certain_words()
'
' Remove_certain_words Macro
' Macro recorded 19/01/2010 by Stodge
'
' Keyboard Shortcut: Ctrl+Shift+R
'
Cells.Select
Selection.Replace What:="systems", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="system", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub

What I can't work out is how to create a UDF to look at cell A1 and remove the words I don't want. In addition, I want to be able to remove plurals from words.

Hope this makes sense.
 
Upvote 0
Plurals would be very difficult as it's not just a question of removing an 's' from the end of a word (and not all words ending in 's' are plurals!)
For a UDF:
Code:
Function CleanWords(rngIn as Range) As String
'
' Remove_certain_words Macro
    Dim strTemp as String
    strTemp = rngIn.Value
    strTemp = Replace(strTemp,"systems", "")
strTemp = Replace(strTemp,"system", "")
strTemp = Replace(strTemp,"-", "")
    CleanWords = strTemp
End Function
 
Upvote 0
Hi

If you have more words you can use an array as an exclusion list:

Code:
Function Remove_certain_words(ByVal s As String)
Dim vArray As Variant, vWord As Variant
 
vArray = Array("systems", "system", "-")
    
For Each vWord In vArray
    s = Replace(s, vWord, "")
Next vWord
Remove_certain_words = s
End Function

Remark: When you initialise the array place the plurals before the singulars and the signs at the end.
 
Upvote 0

Forum statistics

Threads
1,216,484
Messages
6,130,936
Members
449,608
Latest member
jacobmudombe

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