Extract some (not all) numbers from these strings

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
364
Office Version
  1. 2019
I have the following output in a csv file that I'd like to extract the number of bottles from.
2022 Sparkling Tempranillo Rosé - 1 Bottle
2022 Limited Edition Sparkling Vermentino Case - 3 Bottles
2022 Sparkling Tempranillo Rosé Case - 6 Bottles
2022 Limited Edition Sparkling Vermentino Case - 12 Bottles

I have used =MID(A2,FIND(" Bottle",A2)-2,2) to get part way there, but I'm left with a leading space in the single digit results and what looks to be text instead of a numeral when I get "12" for the last example.

I've simply run out of brain power to get any further. Can anyone help?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Check this and revert - This shall replace space and convert the result to number

Excel Formula:
=SUBSTITUTE(MID(A2,FIND(" Bottle",A2)-2,2)," ","")*1
 
Upvote 0
Here is another way of solving it, but it's really no different from Sanjay's solution - it just trims result (ie, removes the leading space) and then converts the resulting string into a value:
Excel Formula:
=VALUE(TRIM(MID(A2,FIND(" Bottle",A2)-2,2)))
 
Upvote 0
There is no need to either TRIM or SUBSTITUTE the space. Your original formula with a +0 would be simpler (assuming the number of bottles is never 100 or more)
Excel Formula:
=MID(A2,FIND(" Bottle",A2)-2,2)+0

BTW, there is a newer version of XL2BB available. You might want to consider updating.
 
Last edited:
Upvote 1
There is no need to either TRIM or SUBSTITUTE the space. Your original formula with a +0 would be simpler (assuming the number of bottles is never 100 or more)
Excel Formula:
=MID(A2,FIND(" Bottle",A2)-2,2)+0

BTW, there is a newer version of XL2BB available. You might want to consider updating.
Thanks Peter. How does this work - does the +0 just force Excel to convert the string result to a number?
 
Upvote 0
does the +0 just force Excel to convert the string result to a number?
Yes, and in doing so automatically removes any leading or trailing spaces, hence my comment above about not needing TRIM or SUBSTITUTE.
Instead of +0 you could also use *1 or also do it like this.
Excel Formula:
=--MID(A2,FIND(" Bottle",A2)-2,2)
 
Upvote 0
That will fail if there are over 99 bottles.

=MAX(IFERROR(--MID(MID(A1,FIND("-",A1)+2,255),1,ROW(INDIRECT("1:15"))),0))
 
Upvote 0
That will fail if there are over 99 bottles.
I did mention that already. ;)
(assuming the number of bottles is never 100 or more)

Given the type of data & the fact that the question was only about getting a numerical value rather than text from the original formula, my guess that >99 will not occur for the OP.
However if that were possible, I provide the following comments.

=MAX(IFERROR(--MID(MID(A1,FIND("-",A1)+2,255),1,ROW(INDIRECT("1:15"))),0))
That will fail if a hyphen occurs before the 'relevant' hyphen as shown in rows 6 & 7 below.
That could be overcome, and the volatility removed, with a formula structure like column J.
Column K offers a slightly shorter alternative.

23 12 18.xlsm
AIJK
1
22022 Sparkling Tempranillo Rosé - 1 Bottle111
32022 Limited Edition Sparkling Vermentino Case - 3 Bottles333
42022 Sparkling Tempranillo Rosé Case - 6 Bottles666
52022 Limited Edition Sparkling Vermentino Case - 12 Bottles121212
62009 Chateau Haute-Madrac - 864 Bottles per crate0864864
72019-20 Some wine case - 6 Bottles066
Bottles
Cell Formulas
RangeFormula
I2:I7I2=MAX(IFERROR(--MID(MID(A2,FIND("-",A2)+2,255),1,ROW(INDIRECT("1:15"))),0))
J2:J7J2=MAX(IFERROR(--MID(MID(A2,FIND(" - ",A2)+3,255),1,{1,2,3,4,5,6}),0))
K2:K7K2=--REPLACE(LEFT(A2,FIND(" Bottle",A2)),1,FIND(" - ",A2)+2,"")


Columns J & K above would also fail if " - " occurred before the 'relevant' " - " as shown below, where yet another alternative is provided should that be possible with the data.

23 12 18.xlsm
AIJKL
92009 Chateau Haute-Madrac - French - Case - 864 Bottles00#VALUE!864
Bottles
Cell Formulas
RangeFormula
I9I9=MAX(IFERROR(--MID(MID(A9,FIND("-",A9)+2,255),1,ROW(INDIRECT("1:15"))),0))
J9J9=MAX(IFERROR(--MID(MID(A9,FIND(" - ",A9)+3,255),1,{1,2,3,4,5,6}),0))
K9K9=--REPLACE(LEFT(A9,FIND(" Bottle",A9)),1,FIND(" - ",A9)+2,"")
L9L9=--RIGHT(SUBSTITUTE(LEFT(A9,FIND(" Bottle",A9))," - ",REPT(" ",15)),15)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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