Parsing variables XXX12 and XXX2 to compare XXX, must be excel function

pawest

Board Regular
Joined
Jun 27, 2011
Messages
105
Hello VBA world... well this question is for Excel Function world:
I am trying to parse a string and return just the text within a string. The text strings are the same but the ending number variations differ. For example, I have two comparable variables "XXX12" and "XXX2" and I'm trying to parse out the XXX.

Factors making this difficult:
  • The beginning text could range from X to XXXXX
  • The ending numbers could be any single or double digit combination on the first variable and any single digit on the second variable, so the first variable could be "XX11" and the second variable could be "XX1" or the first "XXXX3" and the second "XXXX3"... the integers at the end of the string will rarely ever follow follow the same pattern
    • Therefore, a simple =FIND() won't work because of the variability of numbers
    • I tried a FIND("XX11",OR(0,1,2,3,4,5,6,7,8,9)) but an OR nested in FIND doesn't work
  • This MUST be some combination of excel formulas and NOT a hardcode derived from VBA

I've tried functions such as LEFT, RIGHT, LEN, ISNONTEXT, ISNUMBER, FIND, CHAR, LEN, OR, SUBSTITUTE, etc... and I'm not having much luck. Please let me know if anyone has encountered this before and if you could help provide a solution.

Thanks!
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
ok so here are a couple of UDFs that will extract the text or the number

Code:
Function ExtractNumber(Target As Range) As Variant
Dim i As Integer
Dim str1 As String

For i = 1 To Len(Target)
    If IsNumeric(Mid(Target, i, 1)) Then
        str1 = str1 + Mid(Target, i, 1)
    End If
Next i
ExtractNumber = str1

End Function
Function ExtractText(Target As Range) As Variant
Dim i As Integer
Dim str1 As String

For i = 1 To Len(Target)
    If Not IsNumeric(Mid(Target, i, 1)) Then
        str1 = str1 + Mid(Target, i, 1)
    End If
Next i
ExtractText = str1

End Function

Excel 2003
AB
3XXX1212
4XXX2XXX
5xxxx234xxxx

<COLGROUP><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
Sheet3

Worksheet Formulas
CellFormula
B3=extractnumber(A3)
B4=ExtractText(A4)
B5=ExtractText(A5)

<THEAD>
</THEAD><TBODY>
</TBODY>

<TBODY>
</TBODY>
 
Upvote 0
Hi

A general solution for a pattern "any number of letters followed by any number of digits", try:

=LEFT(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&1234567890))-1)

You may write a simpler formula if you make it specific to your case.
 
Upvote 0
texasalynn, great recommendations on the functions... I may end up using those. However, this spreadsheet needs to go in the hands of many other people that don't know VBA. While a function created in VBA shouldn't matter, I would prefer to do it without any VBA whatsoever. Nonetheless, those parsing functions are awesome!

Ron Coderre, here's some example data and what I'm trying to achieve:

Variable 1Variable 2Desired Match
XXA12
XXA2XXA
XXB9XXB9XXB
XC3XC3XC
XXXD11XXXD1XXXD
XXXXE10XXXXE10XXXXE

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


Here's one solution I've come up with but this is certainly not user-friendly and it's way too bulky: =IF((LEFT(T13,6)=LEFT(X13,6)),LEFT(X13,6),IF((LEFT(T13,5),LEFT(X13,5)),LEFT(X13,5), … down to ... LEFT(T13,1),LEFT(X13,1)),LEFT(X13,1),)

Thanks!
 
Upvote 0
Pgc01,
I like your proposed solution a lot but I'm not getting it to work. Here was a test sample that I tried: =LEFT(T18,LEN(FIND({0,1,2,3,4,5,6,7,8,9},X18))-1)... it's returning #VALUE and I'm trying to figure out why. I tried to switch min to len but that didn't work either.
 
Upvote 0
I tried this for further testing and it worked: =FIND({2},X18) ... this worked because I was searching for a 2 in the text string for this particular instance.

When I tried this to add some variability: =FIND({0,1,2},X18) ... it did not work and returned the #Value error, I even tried making the whole thing an array and that didn't work either

Any other suggestions are welcomed as always!
 
Upvote 0
using pgc01 formula works


Excel 2003
AC
3XXX12XXX
4XXX2XXX
5xxxx234xxxx
Sheet3
Cell Formulas
RangeFormula
C3=LEFT(A3,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A3&1234567890))-1)
 
Upvote 0
Pgc01,
I like your proposed solution a lot but I'm not getting it to work. Here was a test sample that I tried: =LEFT(T18,LEN(FIND({0,1,2,3,4,5,6,7,8,9},X18))-1)... it's returning #VALUE and I'm trying to figure out why. I tried to switch min to len but that didn't work either.

Hi

That was not the formula I posted. For ex, for T18


=LEFT(T18,MIN(FIND({0,1,2,3,4,5,6,7,8,9},T18&1234567890))-1)

Remark: I don't understand why you have to look at variables 1 and 2, since you always want the alpha part, that is the same in both.
 
Upvote 0
texasalynn and pgc, I now understand the function a lot better and I got it working! Thanks a lot!

One more question: could I add unique characters to this, as well? For example, I would like to do something like this: =LEFT(X13,MIN(FIND({0,1,2,3,4,5,6,7,8,9,"_","@"},T13&1234567890_@))-1) ... It seems like that's not working because the MIN function may not understand the "_","@" ... any feedback would be great! Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,263
Members
449,149
Latest member
mwdbActuary

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