Extract 4 character string

pto160

Active Member
Joined
Feb 1, 2009
Messages
473
Office Version
  1. 365
Platform
  1. Windows
I'm trying to extract a 4 character string that can be numbers or a text string. Here is the example.

Sheet1

AB
1 Result
2abc rtyidf 1034 567
1034
3fg abcd 789 1464654abcd
4erd f rtyurtyu

<colgroup> <col style="WIDTH: 30px; FONT-WEIGHT: bold"> <col style="WIDTH: 126px"> <col style="WIDTH: 64px"></colgroup> <tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

How do you do it?
 
Peter_SSs, duly noted about providing more concrete examples. It probably is a good idea to provide examples that are closer to the type of data you use at work. Thanks for the tip.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Rick, I also used your UDF and it works very good.

Is there a way to change it so it extracts the character string without looking for batch as the starting point?
Sure, the "batch" requirement is there only because you requested it. Here is the code from Message #35 modified to remove the "batch" requirement...
Code:
Function Chars(S As String, Num As Long) As String
  Dim X As Long, Txt() As String
  Txt = Split(S)
  For X = 0 To UBound(Txt)
    If Txt(X) Like Application.Rept("[A-Za-z0-9]", Num) Then
      Chars = Txt(X)
      Exit Function
    End If
  Next
End Function
 
Upvote 0
Sure, the "batch" requirement is there only because you requested it. Here is the code from Message #35 modified to remove the "batch" requirement...
Code:
Function Chars(S As String, Num As Long) As String
  Dim X As Long, Txt() As String
  Txt = Split(S)
  For X = 0 To UBound(Txt)
    If Txt(X) Like Application.Rept("[A-Za-z0-9]", Num) Then
      Chars = Txt(X)
      Exit Function
    End If
  Next
End Function
Actually, the above code can be simplified slightly...
Code:
Function Chars(S As String, Num As Long) As Variant
  For Each Chars In Split(S)
    If Chars Like Application.Rept("[A-Za-z0-9]", Num) Then Exit Function
  Next
  Chars = ""
End Function
 
Upvote 0
Another version for the "batch"-less case:

=MID(" "&A2&" ",SEARCH(" ???? "," "&A2&" ",MATCH(4,LEN(SUBSTITUTE(MID(" "&A2&" ",SEARCH(" ???? "," "&A2&" ",ROW($1:$150)),6)," ","")),0))+1,4)
 
Upvote 0
Rick, I'll be using both UDF'S. The batch string has 2 4 character word or numbers so it will help me get the second word or number. The other UDF will be for strings that don't have batch.

The UDF'S work great. This will save me a lot of time. It's unbelievable what Excel can do.

Thanks so much for the formula István Hirsch. It works great.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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