Partial deletion

ExcelVixen

New Member
Joined
Jul 21, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi. I am trying to delete a word in a string in column A, if the word is found in column B. how do i delete only the specific word found in the string and not the entire string in the cell? E.g. Cell A1 = The cat ate the rat ; Cell B1 = rat. I just want to delete the word "Rat" in A1. A1 result = The cat ate the ; The values differ in each cell in both columns. see image attached.
1658399674977.png


I have tried find and replace but it is tedious for thousands of rows of data.
thanking you in advance.
N
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Welcome to the MrExcel board!

Can you confirm the expected outcomes below
Row 2: The and the do not match exactly
Row 3: rat is inside bratty

22 07 21.xlsm
AB
1
2The cat satthe
3Tom was brattyrat
Sheet2 (3)
 
Upvote 0
Hi. it must not be a partial recognition. "Rat" inside of "bratty" must be ignored. the case is irrelevant.
 
Upvote 0
Here are a few possible options.
  1. Formula as in column C below. Some possible 'case' issues in the results? (eg C4 and C6)
  2. IF you have the TEXTSPLIT function (not too many users do have it yet) then column D formula.
  3. This user-defined function below and as used in column E. Post back if you want to investigate this option further and don't know how.
VBA Code:
Function SubWord(orig As String, subs As String) As String
  With CreateObject("VBScript.RegExp")
    .IgnoreCase = True
    .Pattern = "\b" & subs & "\b"
    SubWord = Application.Trim(.Replace(orig, ""))
  End With
End Function

ExcelVixen.xlsm
ABCDE
1OriginalWordResultResultResult
2the cat ate the ratratthe cat ate thethe cat ate thethe cat ate the
3the house is bluebluethe house isthe house isthe house is
4Once upon a time thetheonce upon a timeOnce upon a timeOnce upon a time
5The cat satthecat satcat satcat sat
6Tom was brattyrattom was brattyTom was brattyTom was bratty
Substitute
Cell Formulas
RangeFormula
C2:C6C2=TRIM(SUBSTITUTE(" "&LOWER(A2)&" "," "&LOWER(B2)&" "," "))
D2:D6D2=LET(ts,TEXTSPLIT(A2," "),TEXTJOIN(" ",1,FILTER(ts,ts<>B2)))
E2:E6E2=SubWord(A2,B2)
 
Last edited:
Upvote 0
Here are a few possible options.
  1. Formula as in column C below. Some possible 'case' issues in the results? (eg C4 and C6)
  2. IF you have the TEXTSPLIT function (not too many users do have it yet) then column D formula.
  3. This user-defined function below and as used in column E. Post back if you want to investigate this option further and don't know how.
VBA Code:
Function SubWord(orig As String, subs As String) As String
  With CreateObject("VBScript.RegExp")
    .IgnoreCase = True
    .Pattern = "\b" & subs & "\b"
    SubWord = Application.Trim(.Replace(orig, ""))
  End With
End Function

ExcelVixen.xlsm
ABCDE
1OriginalWordResultResultResult
2the cat ate the ratratthe cat ate thethe cat ate thethe cat ate the
3the house is bluebluethe house isthe house isthe house is
4Once upon a time thetheonce upon a timeOnce upon a timeOnce upon a time
5The cat satthecat satcat satcat sat
6Tom was brattyrattom was brattyTom was brattyTom was bratty
Substitute
Cell Formulas
RangeFormula
C2:C6C2=TRIM(SUBSTITUTE(" "&LOWER(A2)&" "," "&LOWER(B2)&" "," "))
D2:D6D2=LET(ts,TEXTSPLIT(A2," "),TEXTJOIN(" ",1,FILTER(ts,ts<>B2)))
E2:E6E2=SubWord(A2,B2)
thank you. it works brilliantly.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

As a matter of interest, which one suits you the best?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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