Whenever we deal with large amounts of data, one of the goals for analysis is,
How is this data distributed?
This is where a Box plot can help. According to Wikipedia, a box plot is a convenient way of graphically depicting groups of numerical data through their five-number summaries: the smallest observation (sample minimum), lower quartile (Q1), median (Q2), upper quartile (Q3), and largest observation (sample maximum) [more]
Quartile?!? What is that like?
When we say $ 39,000 is the lower quartile of salaries paid in Acme inc. it means, 25% of people make less than or equal to $39,000
Like that Median (Q2) means half the samples are lower than median & the other are more than median.
Example Box Plot
Here is an example box plot depicting salaries of all analysts in USA as per our recent Excel Salary Survey.

The box shows distribution of middle half of data (salaries) while the lines (called as whiskers) show minimum and maximum salaries.
As you can see, 50% of the analysts make between $46,000 to $75,000 while the min is $10k and max is $160k.
Why use Box plots?
Box & whisker plots are an excellent way to show distribution of your data without plotting all the values. They are easy to understand. We can use them whenever we have lots of data or dealing with samples drawn from larger population.
Creating Box plots in Excel – 9 step tutorial
Despite their utility, Excel has no built-in option to make a box plot. Of course you can make a 3D pie chart or stacked horizontal pyramid chart. Lets save them for your last day at work and understand how to create box plots in Excel.
Step 1: Calculate the number summaries
Assuming your data is in list use formulas MIN, MAX & PERCENTILE to calculate summaries like below:

To calculate 25th percentile (Q1) use = PERCENTILE(list, 25%)
Step 2: Make a bar chart from Q1, Median & Q3
Just select the 25th percentile, median & 75th percentile values and create a bar chart.Make sure that your chart shows 3 different colored bars not 3 bars in one color.

Step 3: Set series overlap to 100%
Select any bar, press CTRL+1 (right click > format series) and adjust series overlap to 100%

Step 4: Adjust series order so that you can see all the bars
If you cannot see all the bars, right click on chart, click on “Select data”.
Now, adjust the series order using arrow keys so that you can see all the bars. See this demo:

Step 5: Make 25th percentile (Q1) bar invisible
Select the bar corresponding to Q1 and fill it with white color. If you make it transparent, it will not work. So make it all white.
Step 6: Add error bars to Q1 & Q3 series
Just select Q1 (25th percentile) bar and add error bar (any type) from layout ribbon.

Repeat for Q3 series as well.

Step 7: Set up error values in your data
Add an extra column in your data area and use simple formulas to calculate error values, like below:

Step 8: Set up custom error values for Q1 & Q3

Select the error bar for Q1 (25th percentile) and,
- Press CTRL+1 to format them
- Enable only minus (negative) error bar with no cap.
- Select Custom as error amount and point to the calculated value.
Repeat for Q3, but choose positive error bar instead.
Step 9: Format the box plot to your taste
Remove any legend, axis, labels that you do not need. Change colors to suit your taste and mood. Make the whiskers subtle and knock off the grid lines. You are good to go.

Making Box plots interactive
Since box plots are very useful to understand distribution of values, we use them in dashboards etc. Naturally, you are interested to know how values are distributed for various things.
In this example, we may want to know how analyst salaries compare with manager salaries.
To make things complicated, we have 10 different job types, thus enabling 45 possible comparisons (10c2)
This is where interactive box plots can help. See this demo to understand:
Interactive Box plot in Excel – a Demo

How to make interactive box plot in Excel
Construction of box plot is same as mentioned above. The difference is in adding interactivity.
Step 1: Use combo box form controls to capture comparison criteria
Excuse the tongue twister. Using Developer ribbon > Insert > Form controls, add 2 combo box controls and point them to the list of job types.
Lets assume that these combo boxes are linked to cells D1, D2.
[Related: Introduction to Excel Form Controls]
Step 2: Calculate 5 number summaries using MINIF, MAXIF and PERCENTILEIF formulas
Don’t rush to type the formulas yet. There is no such formula as MINIF (or MAXIF or PERCENTILEIF). Assuming your list of jobs are in joblist, write
=MIN(IF(joblist=”Analyst”, list_of_values,””))
and press CTRL+Shift+Enter
Using MAX(IF(…)) and PERCENTILE(IF(…)) you can calculate remaining 4 summaries.

Step 3: Based on combo box selection, fetch any two sets of values
Using INDEX formula, we can fetch values corresponding to each combo box selection to a set of cells, like this:

Step 4: Connect these values to your box plots
That simple!
Step 5: Format and interact
Format the charts. Play with combo boxes to interactively compare one set of distribution with another. Show it to your boss or client and see them fall off a chair.
Download Box plot tutorial workbook
Click here to download the workbook containing these examples. Play with it. Check out various formulas and chart settings. Learn.
Do you use Box plots?
I love box plots. I have used them several times. Few examples are here: Excel age survey results, Gantt box chart and more.
In our Excel salary survey contest too, many people have used box plots to clearly compare compensation composition. Checkout the entries by Aditya, Allred, Anchalee, Anup, Bryan, Jeanmarc, Joerg, Kostas, Luke, Michael, Nathan, Sergey and Vishwanath. Especially Jeanmarc used interactive version of box plots to allow comparison on demand.
What about you? Do you use Box plots often? How do you prepare them? What is your experience like? Please share using comments.
Create Box plots often? Use Jon’s Add-in
If you need to create box plots often and find the above process tedious, then please consider getting a copy of Jon Peltier’s Box Plot add-in for Excel. It works like a charm and produces what you need. All in a few clicks. Click here to know more.
PS: Link to Jon’s add-in is an affiliate link. It means, when you buy it from Jon thru this link, I will get a few bucks too. I recommend it because I know it is awesome and perfect for box plots.













32 Responses to “Extract Numbers from Text using Excel VBA [Video]”
Interesting that you are posting this at the same time as Doug http://yoursumbuddy.com/regex-function-sum-numbers-string/
Looks like two different articles about two different subjects, extracting numbers in text vs. summing all the numbers in text. Also, articles are published 20 days apart. Is the interesting part that there were two articles written about Visual Basic techniques within this month?
Sorry, that should have said 1 day, not 20. Was looking at the wrong thing. I still think it's just a nice coincidences to have multiple articles about VB written. Dick Kusleika also routinely writes about VB at dailydoseofexcel.com
What a lucky coincidence. I know about Doug's blog, but havent had a chance to read it in a while. Thanks for sharing the link.
I think that the best lesson that can come from the several salary survey solutions is that one should have anticipated the variety of monetary units. If the survey utilized drop down currency lists and limited the salary field to whole numbers only, etc. the resulting input would have been far cleaner. Sorry, Chandoo, but the messy input was, in my opinion, self-inflicted.
You are right. Since there are more than 200 different currencies, I thought a currency field would complicate the survey. The bigger problem was, Google Docs (which I used for survey) does not have an option to capture only numbers. Input fields were by text, so people entered in lots of different formats.
But I am happy how it turned out. It taught me several lessons on how to clean data.
Next time I will use a better tool to capture such responses.
Your post made me check how the "regular" and "irregular" decimal separators look like in different countries and it appears to be really interesting case. Take a look:
http://en.wikipedia.org/wiki/Decimal_mark
Cheers.
I am pretty sure you can replace this code block from your article...
If Text Like "*.*,*" Then
european = True
Else
european = False
End If
with this single line of code...
european = Format$(0, ".") = ","
Just to follow up on my previous post, I think I may have misunderstood the intent of your code. You were not looking to see if the computer system was using a dot for the decimal point, rather, you were looking to see if the Text was using a dot as the decimal point, weren't you? If so, then you could use this single line of code as to replace your If..Then..Else block...
european = Text Like "*.*,*"
But what if the number in Text was not large enough to display a thousands separator? Or what if it were a whole number? In either of those cases your original test, and my replacement for it, will fail. Maybe this would be a better test...
european = Right(Format$(Text, "."), 1) = ","
You are right. I am checking if the text has European format. And I loved your one line shortcut. I did not think of using LIKE in such context. Thanks for sharing that.
Again, you are right that this method would fail if the number is not big enough for a thousands separator. Since my data has annual salaries, all numbers are usually in thousands. So I did not think about it.
Hi ,
I have a question please. I'm working on a report that has alphanumeric on it and I only need to retrieve 7 integers that starts with 7 and 3 example SCM RIS PX RIS 02 - 7152349, ADSF\243434134, CM532345 and i need to get the 7152349. Can you please help me on this? I truly appreciate your help!
Thank you very much!
Hi-
The post was wonderful. Please take a look at this function also
Function ExtractNumber(InputString As String) As String
'Function evaluates an input string character by character
' and returns numeric only characters
'Declare counter variable
Dim i As Integer
'Reset input variable
ExtractNumber = ""
'Begin iteration; repeat for the length of the input string
For i = 1 To Len(InputString)
'Test current character for number
If IsNumeric(Mid(InputString, i, 1)) Then
'If number is found, add it to the output string
ExtractNumber = ExtractNumber & Mid(InputString, i, 1)
End If
Next i
End Function
Thank you so much. Your function code is amazing. It very useful for my lesson. Thank you so much.
To be more international.
At the beginning, for the rench format :
If fromThis.Value Like "*.*,*" Or fromThis.Value Like "* *,*" Then
european = True
End If
And at the end :
ElseIf ltr = "," And european And Len(retVal) > 0 Then
retVal = retVal & Application.DecimalSeparator
End If
Hi Chandoo,
Sorry, but your code does not work correctly with my Hungarian excel. My decimal separator is "," so
getNumber = CDbl(retVal)
will not convert the string to value, because you hard-coded "." as separator.
And, as you mentioned: "method would fail if the number is not big enough for a thousands separator" I would like to add: would fail if the user did not enter the thousand separator and also would fail if the thousand separator is not "," nor "." but " " (space chr) - as in Hungary.
This two functions could help to determine the system settings:
application.DecimalSeparator
application.ThousandsSeparator
Conclusion:
you say: "We do not need special treatment for regular format (61,000.30) as Excel & VBA are capable of dealing with these numbers by default." - it is true in case you system uses the regular format. 🙂
Cheers,
Kris
Awesome! It works !!
But how does one take into account negative numbers (say the list has negative numbers and I want to retain those negative numbers)
Thanks.
Hi. When I download this example, my excel is not showing formulas exactly. I wanted a ready version of this example, please. Thank you
Hi Chandoo,
Thanks for this brilliant article like many others that you have written for the benefit of many. Unfortunately, I am constantly having problems downloading your sample workbooks. I am currently using Excel 2007, and each time I try to download any of your sample workbooks, for e.g. the 'Extract Numbers Using VBA workbook', I get the following message 'This file is not in a recognizable format'.
I always get this message each time I try to download any of your sample workbooks. Please kindly advise me on how to resolve this.
Thank you.
Kenny
I have numbers like 12345-12-1 which I want to extract from text strings. 12345 might be variable there as 123, 1234, 12345, 123456,1234567 or so. When I get that in other cell (Column) I should see multiple entries of similar numbers with - (hyphen). How to do that?
@Madhav
Assuming your data is in cell A1
=LEFT(A1,FIND("-",A1)-1)
Thanks Hui for your response. Thank you for your time to find potential solution for my problem.
I tried your formula but was not successful in using the same.
here is more clarification so that you/others could help me.
Column A has following in Cells A1 to A4.. could be long..
ABCD 12345-12-1 XYZ 9878-02-9
LMNOPQ 12345-12-1 STQ 789748-98-5
NFHFKDJFKDS 123-23-1, NDKANSD
A FDSAFNDS 12345-12-1, ASNDSAND
from such data I need to extract the number with hyphens
remove , immediately after the numbers, separate the numbers with spaces
Column B shall look like:
12345-12-1 9878-02-9
12345-12-1 789748-98-5
123-23-1
2345-12-1
2 separate strings (numbers) having hyphen (-) therein should be separated with space.
@Madhev
Have a look at a solution using a simple UDF
https://www.dropbox.com/s/zexf4t9tmxmt3m9/Get_Numbers.xlsm?dl=1
Thanks Hui that worked well with the examples I provided.
I should have given following type of example:
2-ABCD 12345-12-1 X-2-YZ 9878-02-9
in the above case I do not want to extract a number and hyphen which is connected to or is part of text string..
Can you please help me modify the code to ignore numbers and - with text string.?
Thanks in advance.
@Madhav
So what is the answer expected from
2-ABCD 12345-12-1 X-2-YZ 9878-02-9
Thanks for your interest and time Hui.
so when I have text like
2-ABCD 12345-12-1 X-2-YZ 9878-02-9 3-abc-4-efg in Cell A2
in B2 the answer should be only numbers with hyphens and no text with numbers or hyphens
12345-12-1 9878-02-9 OR
12345-12-1 some delimiter (, or 😉 9878-02-9
The logic I thought was (but unable to do)
1. remove all strings containing text (and - and numbers) and then extract only numbers containing hyphens
2. Extract numbers in only following format ( # is a digit below) and ignore numbers and hyphens in any other format
#######-##-#
######-##-#
#####-##-#
####-##-#
###-##-#
##-##-#
Hope this helps.
Why not just use the function =getNumber ?
=getnumber doesn't extract numbers with hyphens..
also need to ignore numbers and hyphens associated with text string
When I use this code that code give me error
cdb1 is not highlight can u explain me
@Deepak
It runs fine for me
Select the first line and Press F9 to set a stop point
goto a cell and edit the function and press Enter
Then you can step through the code when it runs using F8
report back what happens
HI,
How can we add spaces between numbers and removing decimals.
how can we make spaces in the reesult e.g 25 655 2335
Dear Team,
I need to extract number (cheque number) from a cell (some numbers may repeat that to be ignored),
Text is - :-Inward Clg Cheque 00992924 00992924,BD
Result should be - 992924
Kindly help in getting formula for this (please email the code or VBA Code)