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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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:
[TABLE="width: 603"]
<colgroup><col></colgroup><tbody>[TR]
[TD]TEXT
[/TD]
[/TR]
[TR]
[TD]A1) received from 123-4567-89 'k Zag twee beren broodjes smeren
[/TD]
[/TR]
[TR]
[TD]A2) t Was een wonder 1234 5678 9102 boven wonder
[/TD]
[/TR]
[TR]
[TD]A3) dat die 123456789 beren smeren konden.
[/TD]
[/TR]
[TR]
[TD]A4) Hi hi hi, ha ha 987-6543-21 ha
[/TD]
[/TR]
[TR]
[TD]A5) k stond erbij en ST12 5467 8741 ik keek ernaar.
[/TD]
[/TR]
[TR]
[TD]A6) Ik zag twee apen VW11 5444 6698 wortelen schrapen
[/TD]
[/TR]
[TR]
[TD]A7) Ik zag twee apen 111-1111-11 tarwe rapen / nootjes rapen
[/TD]
[/TR]
[TR]
[TD]A8) Ik zag twee vlooien 252555548 mutsjes plooien

Column B (result)
[TABLE="width: 152"]
<colgroup><col></colgroup><tbody>[TR]
[TD]ACCOUNT NUMBER
[/TD]
[/TR]
[TR]
[TD]B1) 123-4567-89
[/TD]
[/TR]
[TR]
[TD]B2) 1234 5678 9102
[/TD]
[/TR]
[TR]
[TD]B3) 123456789
[/TD]
[/TR]
[TR]
[TD]B4) 987-6543-21
[/TD]
[/TR]
[TR]
[TD]B5) ST12 5467 8741
[/TD]
[/TR]
[TR]
[TD]B6) VW11 5444 6698
[/TD]
[/TR]
[TR]
[TD]B7) 111-1111-11
[/TD]
[/TR]
[TR]
[TD]B8) 252555548
[/TD]
[/TR]
</tbody>[/TABLE]


[/TD]
[/TR]
</tbody>[/TABLE]
 
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,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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