Extract numbers/letters from a line of text with a twist

mattyj7183

New Member
Joined
Dec 28, 2015
Messages
14
Office Version
  1. 365
Platform
  1. Windows
I've read a few posts trying to solve this problem, but I think mine may be a little different than the ones I have seen so far. And please, by all means, if this has already been answered please guide me.

Essentially I have strings of text about 5000+ rows long with names, account numbers and other identifying info, but the task is to extract just the account numbers. Examples below all in A1:

PPC, Inc. (12345678, Corporate)
Smith Capital (ABCD Mod Cons), Raymond (98765432, Trust)
Jones, Rich E. & Jane A Doe (87654321, Exect)
Doe Family Trust (Stock), Joe & Jayne (11-F222-77-8, Trust)
Smith, II, Joe(122112288, Indiv (Crawford))
Jones, II, Steve(1234567),
Jones, II 2012 Trust, Josh R. (11112222, Trust)


First I tried text to columns using the "(" as the first delimiter and then the "," as the second. This worked on a good number, but the 5th Smith example I showed resulted in "Crawford))" showing in C3 which is not ideal. In some cases like the 2nd example it put part the account number in C3.

I also tried the GetNumber UDF I've seen floating around but that (as the name suggests) only grabs numbers but as you can see the 4th example has letters and numbers. It also automatically concatenated the numbers removing the "-" between each number. Both results not ideal.

Let me know if you need any further info.

Thanks in advance!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Do your account numbers always start with a digit as your examples show?
 
Upvote 0
Rick - yes they do, at least for now.
I am a little concerned by your "at least for now" statement because if your account number could ever start with a letter (I could work around a symbol like # for example), the following would no longer work. However, as long as the account number starts with a digit, this should work for you...
Code:
Function AcctNums(S As String)
  Dim X As Long, Parts() As String
  Parts = Split(Replace(S, ")", ","), "(")
  For X = 0 To UBound(Parts)
    If Parts(X) Like "#*,*" Then
      AcctNums = Split(Parts(X), ",")(0)
      Exit Function
    End If
  Next
End Function
 
Upvote 0
Thank you for your reply, but unfortunately I am not totally sure where to put this. Is this function a macro or a UDF?
 
Upvote 0
Thank you for your reply, but unfortunately I am not totally sure where to put this. Is this function a macro or a UDF?
It is a function that can be used as a UDF (which is the abbreviation for User Defined Function).

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use AcctNums just like it was a built-in Excel function. For example,

=AcctNums(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

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