VBA: Copy a string pattern with wildcards from an array to a new column

Bart1980

New Member
Joined
Sep 24, 2016
Messages
8
Hi,

I am stuck on this one so any help is more than welcome :D

I have a column with text and I need to copy any account numbers to a new column. The account numbers vary so I have to use wildcards, but the other problem is that the account numbers can have 3 different patterns (hyphen (example 1), empty space between numbers (example 3)).

For example text ... 123-45678-90 (account number) ... text ... 123456789 (account number) ... text .. 1234 5678 9012 3456 (account number)... text.

I think you have to use the LIKE operator with wildcards but with the hypen and empty space + copy to a new column thingy is just a bit too difficult for me. The alternative is reading a bunch of records every day and mannualy copy paste them :s
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Assuming your account numbers are in column A starting in row 1, place this formula in cell B1 and copy it down the column as far as there is data in column A.
=SUBSTITUTE(SUBSTITUTE(A1,"-","")," ","")
 
Last edited:
Upvote 0
Is the question you need answered
"is the given string an account number (in one of the three formats)?"
or is the question
"are the two given strings the same account number, perhaps in different formats?"
 
Last edited:
Upvote 0
Is the question you need answered
"is the given string an account number (in one of the three formats)?"
or is the question
"are the two given strings the same account number, perhaps in different formats?"

The question needed to be answered is "is the given string in the text an account number (in one of the three formats)" if the answer is yes it needs to be copied to another column...
 
Upvote 0
Welcome to the MrExcel board!

Could you give us 6-8 varied 'dummy' data and the corresponding expected results?

My signature block below has a link for suggestions for posting data that we can not only see the layout of rows & columns, but we can also copy the data to test with.
 
Upvote 0
Welcome to the MrExcel board!

Could you give us 6-8 varied 'dummy' data and the corresponding expected results?

My signature block below has a link for suggestions for posting data that we can not only see the layout of rows & columns, but we can also copy the data to test with.

Hello,

The addin doesn't seem to work, gives the error 91 ... With..

but her is an example of what it should do.

Column A:
TEXT
A1) received from 123-4567-89 'k Zag twee beren broodjes smeren
A2) t Was een wonder 1234 5678 9102 boven wonder
A3) dat die 123456789 beren smeren konden.
A4) Hi hi hi, ha ha 987-6543-21 ha
A5) k stond erbij en ST12 5467 8741 ik keek ernaar.
A6) Ik zag twee apen VW11 5444 6698 wortelen schrapen
A7) Ik zag twee apen 111-1111-11 tarwe rapen / nootjes rapen
A8) Ik zag twee vlooien 252555548 mutsjes plooien

Column B (result)
ACCOUNT NUMBER
B1) 123-4567-89
B2) 1234 5678 9102
B3) 123456789
B4) 987-6543-21
B5) ST12 5467 8741
B6) VW11 5444 6698
B7) 111-1111-11
B8) 252555548

<colgroup><col></colgroup><tbody>
</tbody>



<colgroup><col></colgroup><tbody>
</tbody>
 
Upvote 0
The addin doesn't seem to work, gives the error 91 ... With..
Which Add-In (there are several)?
Where was the error?
What version of Excel are you using?


In any case, those samples should be sufficient this time.

I have written this as a user-defined function but could be just a macro that does the job if you want.

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 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)

Code:
Function AccNo(s As String) As String
  Static RX As Object
  
  If RX Is Nothing Then Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(^| )([a-zA-Z]*\d[\d \-]*\d)(?=[^\d]|$)"
  If RX.Test(s) Then AccNo = RX.Execute(s)(0).SubMatches(1)
End Function


Excel 2010 32 bit
AB
1TEXTACCOUNT NUMBER
2received from 123-4567-89 'k Zag twee beren broodjes smeren123-4567-89
3t Was een wonder 1234 5678 9102 boven wonder1234 5678 9102
4dat die 123456789 beren smeren konden.123456789
5Hi hi hi, ha ha 987-6543-21 ha987-6543-21
6k stond erbij en ST12 5467 8741 ik keek ernaar.ST12 5467 8741
7Ik zag twee apen VW11 5444 6698 wortelen schrapenVW11 5444 6698
8no account number
9
10Ik zag twee apen 111-1111-11 tarwe rapen / nootjes rapen111-1111-11
11Ik zag twee vlooien 252555548 mutsjes plooien252555548
AccNo
Cell Formulas
RangeFormula
B2=AccNo(A2)
 
Upvote 0
Peter,

Thank you very much, I'll try it as soon as i get to work tomorrow!
Have a nice Sunday.

Kind regards,

Bart

PS Seeing your code makes me dazzle and realize that I am just a newbie at VBA :s
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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