How will I do Excel user-defined function to extract letters from string

W

Wavit11

How will I extract letters from this string
like... to:

AB-123456-45 AB
BCD-678901-23 BCD
E-23454 E

if I this bottom code extract numbers from a the same string:

Function ExtractNum(rCell As Range)
Dim iCount As Integer, i As Integer
Dim sText As String
Dim lNum As String

sText = rCell

For iCount = Len(sText) To 1 Step -1
If IsNumeric(Mid(sText, iCount, 1)) Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
End If

If i = 1 Then lNum = CInt(Mid(lNum, 1, 1))
Next iCount

ExtractNum = CLng(lNum)
End Function
 
M

Max

Perhaps this would suffice:

Assuming your sample data is in col A, A1, down

AB-123456-45
BCD-678901-23
E-23454
etc

and the data structure remains similar to the sample

Try in B1: =LEFT(TRIM(A1),SEARCH("-",TRIM(A1))-1)

Copy B1 down
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top