Extracting particular data in substring but location is never the same place

kgarland17

New Member
Joined
Jun 17, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I have tens of thousands of SKUs and I need to be able to pull a particular attribute from them all into one column but the attribute is never in the same location across all the SKUs.
How does/can one pull a 1-3 digit number w/ the same letter after it?

example: need to extract the bold data
REST-B-AQ-3FD-6Z-32V-DE-XX
VRZA-B-16Z-29SR-1G-HR-XS-XX
DELI-B-XP-F5S-5W-22-BW-155Z-XX
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
3 examples out of 10,000 doesn't provide a lot of info to work with. You'd have to identify a reliable logical pattern. From what I can see, the approach might be to use vba to:
- loop over each character in the string and find a z.
- if the preceding character is a number (16Z), find the preceding "-". Grab everything between the - and the z.
- if preceding character is not a number (VRZA), find the next z. Rinse and repeat.

If there can never be a z after the z that is preceded with a number then it might be easier to start a search from the right instead of the left.
 
Upvote 0
according to the sample data, please try below formula:
Book2
ABC
1REST-B-AQ-3FD-6Z-32V-DE-XX6Z
2VRZA-B-16Z-29SR-1G-HR-XS-XX16Z
3DELI-B-XP-F5S-5W-22-BW-155Z-XX155Z
4
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=LET(s,TEXTSPLIT(A1,"-"),FILTER(s,(RIGHT(s)="z")*ISNUMBER(-LEFT(s,LEN(s)-1))))
 
Upvote 0
A better set of sample data would have been helpful. However, based on what you posted, this single formula should process your entire range (just change the range reference to your actual data)...

=TEXTAFTER(TEXTBEFORE(A1:A3&"-","Z-",-1),"-",-1)&"Z"
 
Upvote 1
A better set of sample data would have been helpful. However, based on what you posted, this single formula should process your entire range (just change the range reference to your actual data)...

=TEXTAFTER(TEXTBEFORE(A1:A3&"-","Z-",-1),"-",-1)&"Z"
And if it is possible that a cell might not have a number followed by a "Z" in it, then use this formula instead...

=LET(c,TEXTAFTER(TEXTBEFORE(A1:A3&"-","Z-",-1),"-",-1),IF(ISNUMBER(-c),c&"Z",""))
 
Upvote 0
Without knowing what the actual data may contain, I can see some possible issues with both post 3 & 5 formulas - see first mini-sheet below.

kgarland.xlsm
ABCDEFG
1REST-B-25Z-2345Z-AQ-3FD-2DECZ-32V25Z2345Z2DECZ2DECZ
2VRZA-B-16Z-29SR-1G-HR-XS-XX16Z16Z
3DELI-B-XP-F5S-5W-22-BW-155Z-XX155Z155Z
4ABC-45Z-123456Z45Z123456Z123456Z
5A-B-C#CALC!
Z
Cell Formulas
RangeFormula
G1:G5G1=LET(c,TEXTAFTER(TEXTBEFORE(A1:A5&"-","Z-",-1),"-",-1),IF(ISNUMBER(-c),c&"Z",""))
B1:D1,B4:C4,B2:B3,B5B1=LET(s,TEXTSPLIT(A1,"-"),FILTER(s,(RIGHT(s)="z")*ISNUMBER(-LEFT(s,LEN(s)-1))))
Dynamic array formulas.


Below is a more long-winded worksheet formula (perhaps it can be shortened?) that covers at least some more possible cases and also a user-defined function that might be considered.

VBA Code:
Function GetZ(s As String, Optional MaxPrefixDigits As Long = 3) As String
  Dim RX As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(^|-)\d{1," & MaxPrefixDigits & "}Z(?=-|$)"
  If RX.Test(s) Then GetZ = Mid(RX.Execute(s)(0), 2)
End Function

kgarland.xlsm
ABC
1REST-B-25Z-2345Z-AQ-3FD-2DECZ-32V25Z25Z
2VRZA-B-16Z-29SR-1G-HR-XS-XX16Z16Z
3DELI-B-XP-F5S-5W-22-BW-155Z-XX155Z155Z
4ABC-45Z-123456Z45Z45Z
5A-B-C  
Z (2)
Cell Formulas
RangeFormula
B1:B5B1=LET(c,MID(A1,SEQUENCE(LEN(A1)),1),s,CONCAT("-",IF(ISNUMBER(-c),1,c),"-"),p,MIN(IFERROR(FIND({"-1Z-","-11Z-","-111Z-"},s),999)),IFERROR(REPLACE(LEFT(A1,FIND("-",s,p+1)-2),1,p-1,""),""))
C1:C5C1=GetZ(A1)
 
Upvote 0
You're welcome. Hope that you can find something that suits your purpose among the several offerings. :)

As mentioned previously, I don't know how varied your data might be but if the 'Z attribute' that you are trying to find could be the first thing in the string, then my suggested user-defined function would return an incorrect result ("6Z' for the last example below). Here is a correction to the UDF to allow for that circumstance.

VBA Code:
Function GetZ(s As String, Optional MaxPrefixDigits As Long = 3) As String
  Dim RX As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "-\d{1," & MaxPrefixDigits & "}Z(?=-|$)"
  If RX.Test("-" & s) Then GetZ = Mid(RX.Execute("-" & s)(0), 2)
End Function

kgarland.xlsm
AC
1REST-B-25Z-2345Z-AQ-3FD-2DECZ-32V25Z
2VRZA-B-16Z-29SR-1G-HR-XS-XX16Z
3DELI-B-XP-F5S-5W-22-BW-155Z-XX155Z
4ABC-45Z-123456Z45Z
5A-B-C 
626Z-3R-B-123G26Z
Z (2)
Cell Formulas
RangeFormula
C1:C6C1=GetZ(A1)
 
Upvote 0
Extract string that ending with “z”, 1~3 digital numbers before "z", return the first matching item and return "" is there is no mathing items:
Book1.xlsx
ABC
1REST-B-25Z-2345Z-AQ-3FD-2DECZ-32V25Z
2VRZA-B-16Z-29SR-1G-HR-XS-XX16Z
3DELI-B-XP-F5S-5W-22-BW-155Z-XX155Z
4ABC-45Z-123456Z45Z
5A-B-C 
626Z-3R-B-123G26Z
72612Z-3R-B-123G 
83R-B-123G-Z 
93R-B-124G-1Z1Z
103R-1234z-124G-23Z23Z
11
Sheet1
Cell Formulas
RangeFormula
B1:B10B1=LET(s,TEXTSPLIT(A1,"-"),@FILTER(s,(RIGHT(s)="z")*ISNUMBER(VDB(,,999,0,--LEFT(s,LEN(s)-1))),""))
 
Upvote 0
Extract string that ending with “z”, 1~3 digital numbers before "z", return the first matching item and return "" is there is no mathing items:
Interesting formula! I have not seen VDB() used before. Could it be tweaked in case these are possible?

kgarland.xlsm
AB
10B-000891Z-36Z000891Z
11B6-1E1Z-123Z1E1Z
Sheet1
Cell Formulas
RangeFormula
B10:B11B10=LET(s,TEXTSPLIT(A10,"-"),@FILTER(s,(RIGHT(s)="z")*ISNUMBER(VDB(,,999,0,--LEFT(s,LEN(s)-1))),""))
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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