Finding complex partial string

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
146
Office Version
  1. 2013
Platform
  1. Windows
Hi All

I am stuck with how to search a complex string that has so many wildcards. The string format is always "B?W/?????/??/??" or "H?W/?????/??/??" where the question marks are always a number, the "B" or "H" are the constant values in 1st character of string, "W" is always 3rd character, and the string can appear anywhere in the cell and may not always have a space before and/or after it.

As an example I was thinking...

VBA Code:
If Sheets(1).Range("A1") = "*B?W/?????/??/??*" then Msgbox "Match"

but that doesn't seem to work.

Any help appreciated on how to detect the string!


Project 1 B6W/12765/01/02 Road repair
H6W/12483/01/17 Project 2 unknown status
approved Project 3 continue status
Project 4 H6W/12483/01/18locked radio soon
B6W/12765/01/04Project 5 further funding
Project 6 aborted report back
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
51,211
Office Version
  1. 365
Platform
  1. Windows
You could employ a user-defined function like this. To implement ..
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 GetStr(s As String) As String
  With CreateObject("VBScript.RegExp")
    .Pattern = "(B|H)\dW/\d{5}/\d{2}/\d{2}"
    If .Test(s) Then GetStr = .Execute(s)(0)
  End With
End Function

cortexnotion.xlsm
AB
1
2Project 1 B6W/12765/01/02 Road repairB6W/12765/01/02
3H6W/12483/01/17 Project 2 unknown statusH6W/12483/01/17
4approved Project 3 continue status 
5Project 4 H6W/12483/01/18locked radio soonH6W/12483/01/18
6B6W/12765/01/04Project 5 further fundingB6W/12765/01/04
7Project 6 aborted report back 
Sheet1
Cell Formulas
RangeFormula
B2:B7B2=GetStr(A2)
 

Forum statistics

Threads
1,144,517
Messages
5,724,817
Members
422,580
Latest member
ScotIain76

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
Top