Search text in a cell for a specific letter/number combination

broncos347

Active Member
Joined
Feb 16, 2005
Messages
293
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet which contains a column that holds some text, and within that text, there is a priority (which could be A, B, C or D plus the letters will have a suffix of 1, 2, 3, 4 or 5) is there a way to search and extract this information out of the cells of the column in question. The priority could be anywhere within the text.

For example, the text could read as follows “Located at End of Main Building, on vehicle access Route, surface drain gully is blocked, this gully is constantly blocked up during wet weather. C1 NB Historic issues with this area and soak away.” In this instance, I would need to extract the priority of C1.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Could you please provide around 5 rows of sample data along with the expected results?
 
Upvote 0
This is my take on what you are asking. To implement this user-defined function ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function PriorityCode(s As String) As String
  Dim RX As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "[A-D][1-5]"
  If RX.test(s) Then PriorityCode = RX.Execute(s)(0)
End Function

broncos347.xlsm
AB
1Located at End of Main Building, on vehicle access Route, surface drain gully is blocked, this gully is constantly blocked up during wet weather. C1 NB Historic issues with this area and soak away.C1
2 
3Nothing here 
4D6 is not a code but D5 isD5
5b2 is not upper case but A2 isA2
Sheet1
Cell Formulas
RangeFormula
B1:B5B1=PriorityCode(A1)
 
Upvote 0
See below

Defect-Monitor Log.xlsx
ABCD
1SiteAction TagAction CommentTag Action Condition / Priority
2Brims StnMonitor-Structural Movement | Subsidence | SettlementTrip hazard due to probable subsidence at foot of ole structure C10 38. B2B2
3Mill StnMonitor-Structural Movement | Subsidence | SettlementBetween LC16 and LC17 low to level causing ponding. A1A1
4Hatfield StnDefect - Wear and Tear Etc Full fracture across ground cover. C1. Faulted April 2022C1
5Hatfield StnDefect-Graffiti/Vandalism Located on station name signage nearest country end of Platform. Vinyl application has been peeled off exposing previous TOC livery markings. C1C1
6Tree StnDefect-Blocked Located at London End of Main Building, on vehicle access Route, surface drain gully is blocked, this gully is constantly blocked up during wet weather. C1 NB Historic issues with this area and soak away.C1
7Tree StnDefect - Wear and Tear Etc Located at entrance end. 1.5m section of railing fencing has reached end of asset life. C1C1
8Mount StnDefect-Structural Movement | Subsidence | Settlement1st beam from C End dropped and others moved away from track by up to 70mm resulting in surface to fall away leaving voids. Faulted 30.07.21Historic problem. C1C1
Sheet1
 
Upvote 0
See below
Thanks for the sample data. My UDF would require a slight change for that sort of data.

VBA Code:
Function PriorityCode(s As String) As String
  Dim RX As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "\b[A-D][1-5]\b"
  If RX.test(s) Then PriorityCode = RX.Execute(s)(0)
End Function

broncos347.xlsm
CD
1Action CommentTag Action Condition / Priority
2Trip hazard due to probable subsidence at foot of ole structure C10 38. B2B2
3Between LC16 and LC17 low to level causing ponding. A1A1
4 Full fracture across ground cover. C1. Faulted April 2022C1
5 Located on station name signage nearest country end of Platform. Vinyl application has been peeled off exposing previous TOC livery markings. C1C1
6 Located at London End of Main Building, on vehicle access Route, surface drain gully is blocked, this gully is constantly blocked up during wet weather. C1 NB Historic issues with this area and soak away.C1
7 Located at entrance end. 1.5m section of railing fencing has reached end of asset life. C1C1
81st beam from C End dropped and others moved away from track by up to 70mm resulting in surface to fall away leaving voids. Faulted 30.07.21Historic problem. C1C1
Sheet2
Cell Formulas
RangeFormula
D2:D8D2=PriorityCode(C2)
 
Upvote 0
SampleData.xlsx
ABCD
1SiteAction TagAction CommentTag Action Condition / Priority
2Brims StnMonitor-Structural Movement | Subsidence | SettlementTrip hazard due to probable subsidence at foot of ole structure C10 38. B2B2
3Mill StnMonitor-Structural Movement | Subsidence | SettlementBetween LC16 and LC17 low to level causing ponding. A1A1
4Hatfield StnDefect - Wear and Tear Etc Full fracture across ground cover. C1. Faulted April 2022C1
5Hatfield StnDefect-Graffiti/Vandalism Located on station name signage nearest country end of Platform. Vinyl application has been peeled off exposing previous TOC livery markings. C1C1
6Tree StnDefect-Blocked Located at London End of Main Building, on vehicle access Route, surface drain gully is blocked, this gully is constantly blocked up during wet weather. C1 NB Historic issues with this area and soak away.C1
7Tree StnDefect - Wear and Tear Etc Located at entrance end. 1.5m section of railing fencing has reached end of asset life. C1C1
8Mount StnDefect-Structural Movement | Subsidence | Settlement1st beam from C End dropped and others moved away from track by up to 70mm resulting in surface to fall away leaving voids. Faulted 30.07.21Historic problem. C1C1
Sheet10
Cell Formulas
RangeFormula
D2:D8D2=MAP(C2:C8,LAMBDA(s,LET(r,ROW(1:5),x,TEXTSPLIT(s,MID(",.?! ",r,1)),@SORT(XLOOKUP(TOCOL({"A","B","C","D"}&r),x,x,""),,-1))))
Dynamic array formulas.
 
Upvote 0
@JvdV
Suggest SEQUENCE(5) instead of ROW(1:5) so formula still returns the same values if a new row is subsequently inserted above.
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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