vba to find specific combination of chars in one column

gint32

Board Regular
Joined
Oct 8, 2014
Messages
139
Hi all
I have a column that contains free text, I need a way (using vba) to find specific chars in this column the format of the text chars will always be like .. one Alpha and 7 numeric(together).....Examples of this is are D1564567, A1235567, all the way through to Z2356457 and if and when found copy this alpha numeric to he adjacent cell on the right. Not all cells will have this so it needs to e able to skip over records that do not contain, any help appreciated
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You did not say what column had your values, so I assumed Column A with the output going to Column B.
Code:
Sub MoveANNNNNNNtoNextColumn()
  Dim R As Long, Data As Variant
  Data = Range("[B][COLOR="#FF0000"]A[/COLOR][/B]1", Cells(Rows.Count, "[B][COLOR="#FF0000"]A[/COLOR][/B]").End(xlUp))
  For R = 1 To UBound(Data)
    If Not Data(R, 1) Like "[A-Z]#######" Then Data(R, 1) = ""
  Next
  Range("[B][COLOR="#FF0000"]B[/COLOR][/B]1").Resize(UBound(Data)) = Data
End Sub
 
Last edited:
Upvote 0
.. another way.
Code:
Sub CheckPattern()
  With Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(AND(ABS(CODE(LEFT(RC[-1]&"" "",1))-77.5)<13,MAX(ABS(CODE(MID(RC[-1]&REPT("" "",8),{2,3,4,5,6,7,8},1))-52.5))<5,LEN(RC[-1])=8),RC[-1],"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0
And here is yet another way to write the code without using a loop...
Code:
[table="width: 500"]
[tr]
	[td]Sub CheckPattern()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Offset(, 1).Value = Evaluate(Replace("IFERROR(IF((ABS(CODE(LEFT(@))-77.5)<13)*(ABS(RIGHT(@,7)-5000000)<5000001)*(LEN(@)=8),@,""""),"""")", "@", .Address))
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
And here is yet another way to write the code without using a loop...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub CheckPattern()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Offset(, 1).Value = Evaluate(Replace("IFERROR(IF((ABS(CODE(LEFT(@))-77.5)<13)*(ABS(RIGHT(@,7)-5000000)<5000001)*(LEN(@)=8),@,""""),"""")", "@", .Address))
  End With
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

thanks, I have ran the suggested script on the following test data and can't get it to work. I find it'll only pull if the cell contents have just these chars combination only.

the only data it get is in line 2 = H0000002 all the rest are blank

faerfarefr H0000001 eargargagagaH0000004fggf
H0000002
dfagagsf H0000003 fdsghdgfhsfgh
324radsfcdf H0000004 dfrtsrwstg
gfffsgbbbbbbbbbbbvcH0000005 fgdgfgfgfgdsdr5646hgfgxhgf
H0000006 fdsgdgf H0000010 vcbxbv H0000012
gfffsgbbbbbbbbbbbvc H0000005fgdgfgfgfgdsdr5646hgfgxhgf
dfgzdgfg H0000008fdgsdgfg H0000001

<colgroup><col></colgroup><tbody>
</tbody>
 
Upvote 0
I have tried your suggestion and get the same..I am thinking that you may not of got what i meant by free text, so heres another go.
What i mean the input user types free text (speech) in each cell within the column A but at some point within the free text and its really up to them (craZy I know, so dont ask) they may put in this reference(A#######) into the cell( but not always), so if they did then i just want to find the cells that have this and drag it out to the next column...and therfore the ones that don't contain this B####### or Z####### will be discarded..as its the reference number that i need for a vlookup
 
Last edited:
Upvote 0
thanks, I have ran the suggested script on the following test data and can't get it to work. I find it'll only pull if the cell contents have just these chars combination only.

the only data it get is in line 2 = H0000002 all the rest are blank

faerfarefr H0000001 eargargagagaH0000004fggf
H0000002
dfagagsf H0000003 fdsghdgfhsfgh
324radsfcdf H0000004 dfrtsrwstg
gfffsgbbbbbbbbbbbvcH0000005 fgdgfgfgfgdsdr5646hgfgxhgf
H0000006 fdsgdgf H0000010 vcbxbv H0000012
gfffsgbbbbbbbbbbbvc H0000005fgdgfgfgfgdsdr5646hgfgxhgf
dfgzdgfg H0000008fdgsdgfg H0000001

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

What should be done for the cells containing more than one reference number (see red highlighted text above)?
 
Upvote 0
Thanks for staying with me on this. Like i said I'll be using a simple vlookup on the ones that are found so might be best to have each one separated and put in a new adjacent cell. If that's at all possible. Thanks again
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,794
Members
448,994
Latest member
rohitsomani

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