Finding one word in a string and outputting the word in the same row

John Br

New Member
Joined
Jan 18, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi

I want to find one word in a cell that may or maynot be in the cell and then put that word in a cell in the same row.

I found the following code which finds the word (in this case "late") so i have the code for this but unfortunately it just dumps the work "late" into row 2 of the selected column and then in each subsequent row.

Does someone have the code to ensure the work is placed in the same row as the cell with the word "Late" in the text string.

The text string can be any legnth and as i said the word "late" may or not be in the string.

Also it is unlikely that the word would be capitilised, but for completeness sake is there code to search for both "late" and "Late"? This is not critical but it would be good to check for both.

I do not want to find later or latest so should I put a space after late to exclude anything else?

VBA Code:
Sub Late()
Dim arry As Variant
Dim Str As Variant
Dim RE As New RegExp



arry = Range("F2:F20000").Value

RE.Pattern = "late"
RE.Global = True
Dim matches As MatchCollection
i = 2


For Each Str In arry
Set matches = RE.Execute(Str)
'Debug.Print RE.Test(Str)
If RE.Test(Str) = True Then
Cells(i, 25) = matches(0)
i = i + 1
End If


Next Str

End Sub
 
Last edited by a moderator:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

Give this a try with a copy of your workbook.

VBA Code:
Sub Late_v2()
  Dim RX As Object
  Dim a As Variant, b As Variant
  Dim i As Long
  
  Const mySearchWord As String = "Late" '<- Change as required
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = True  '<- Change to false if you want a case-sensitive search
  RX.Pattern = "\b" & mySearchWord & "\b"
  a = Range("F2", Range("F" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If RX.Test(a(i, 1)) Then b(i, 1) = mySearchWord
  Next i
  Range("Y2").Resize(UBound(b)).Value = b
End Sub

My sample data and results:
John Br.xlsm
FY
1
2In the same row as the cell with the word Late in the text stringLate
3See you later
4Do not be late!Late
5
6other text
7Better late than neverLate
8His work was slated by the critics
9Late LATER latestLate
Sheet1


BTW, IF you don't have punctuation beside the word (like row 4 below) then you can also do this by a single formula in the top cell like this.
John Br.xlsm
FZ
1
2In the same row as the cell with the word Late in the text stringLate
3See you later
4Do not be late!
5
6other text
7Better late than neverLate
8His work was slated by the critics
9Late LATER latestLate
Sheet1
Cell Formulas
RangeFormula
Z2:Z9Z2=IF(ISNUMBER(SEARCH(" late "," "&F2:F9&" ")),"Late","")
Dynamic array formulas.
 
Upvote 0
I would like to thank Peter SSs, this has litteraly saved me days of work each month. I did not use the arry formula as sometimes there are punctuation marks after one of the words I want to search for.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,216,500
Messages
6,131,016
Members
449,615
Latest member
Nic0la

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