{"id":3210,"date":"2021-04-12T11:20:34","date_gmt":"2021-04-12T18:20:34","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3210"},"modified":"2021-04-12T11:22:28","modified_gmt":"2021-04-12T18:22:28","slug":"extract-numbers-from-a-string-in-excel-using-formulas-or-vba","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3210","title":{"rendered":"Extract Numbers From a String in Excel (Using Formulas or VBA)"},"content":{"rendered":"\n<p>By&nbsp;Sumit Bansal<\/p>\n\n\n\n<p>There is no inbuilt function in Excel to extract the numbers from a string in a cell (or vice versa \u2013 \u00a0remove the numeric part and extract the text part from an alphanumeric string).<\/p>\n\n\n\n<p>However, this can be done using a cocktail of&nbsp;Excel functions&nbsp;or some simple&nbsp;VBA code.<\/p>\n\n\n\n<p>Let me first show you what I am talking about.<\/p>\n\n\n\n<p>Suppose you have a data set as shown below and you want to extract the numbers from the string (as shown below):<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/v1.nitrocdn.com\/zciqOWZgDznkuqFpvIJsLUeyHlJtlHxe\/assets\/static\/optimized\/rev-029371b\/wp-content\/uploads\/2017\/08\/Extract-Number-from-String-in-Excel-Data.png\" alt=\"Extract Number from String in Excel - Data\" class=\"wp-image-29591\"\/><\/figure><\/div>\n\n\n\n<p>The method you choose will also depend on the version of Excel you\u2019re using:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For versions prior to Excel 2016, you need to use slightly longer formulas<\/li><li>For Excel 2016, you can use the newly introduced TEXTJOIN function<\/li><li>VBA method can be used in all the versions of Excel<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Extract Numbers from String in Excel (Formula for Excel 2016)<\/h2>\n\n\n\n<p>This formula will work only in Excel 2016 as it uses the newly introduced TEXTJOIN function.<\/p>\n\n\n\n<p>Also, this formula can extract the numbers that are at the beginning, end or middle of the text string.Note that the TEXTJOIN formula covered in this section would give you all the numeric characters together. For example, if the text is \u201cThe price of 10 tickets is USD 200\u201d, it will give you 10200 as the result.<\/p>\n\n\n\n<p>Suppose you have the dataset as shown below and you want to extract the numbers from the strings in each cell:<\/p>\n\n\n\n<p>Below is the formula that will give you numeric part from a string in Excel.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"828\" height=\"295\" src=\"https:\/\/SUMMALAI.COM\/wp-content\/uploads\/2021\/04\/WeChat-Image_20210412111831.png\" alt=\"\" class=\"wp-image-3212\" srcset=\"https:\/\/SUMMALAI.COM\/wp-content\/uploads\/2021\/04\/WeChat-Image_20210412111831.png 828w, https:\/\/SUMMALAI.COM\/wp-content\/uploads\/2021\/04\/WeChat-Image_20210412111831-300x107.png 300w, https:\/\/SUMMALAI.COM\/wp-content\/uploads\/2021\/04\/WeChat-Image_20210412111831-768x274.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">=TEXTJOIN(\"\",TRUE,IFERROR((MID(A2,ROW(INDIRECT(\"1:\"&amp;LEN(A2))),1)*1),\"\"))<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/v1.nitrocdn.com\/zciqOWZgDznkuqFpvIJsLUeyHlJtlHxe\/assets\/static\/optimized\/rev-029371b\/wp-content\/uploads\/2017\/08\/Formula-to-get-all-number-from-a-string.png\" alt=\"Formula to get all number from a string\" class=\"wp-image-29593\"\/><\/figure><\/div>\n\n\n\n<p>This is an array formula, so you need to use \u2018<strong>Control + Shift + Enter<\/strong>\u2018 instead of using Enter.<\/p>\n\n\n\n<p>In case there are no numbers in the text string, this formula would return a blank (empty string).<\/p>\n\n\n\n<p><strong>How does this formula work?<\/strong><\/p>\n\n\n\n<p>Let me break this formula and try and explain how it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ROW(INDIRECT(\u201c1:\u201d&amp;LEN(A2))) \u2013 this part of the formula would give a series of numbers starting from one. The&nbsp;LEN function&nbsp;in the formula returns the total number of characters in the string. In the case of \u201cThe cost is USD 100\u201d, it will return 19. The formulas would thus become ROW(INDIRECT(\u201c1:19\u201d). The&nbsp;ROW function&nbsp;will then return a series of numbers \u2013&nbsp;{1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19}<\/li><li>(MID(A2,ROW(INDIRECT(\u201c1:\u201d&amp;LEN(A2))),1)*1) \u2013 This part of the formula would return an array of #VALUE! errors or numbers based on the string. All the text characters in the string become #VALUE! errors and all numerical values stay as-is. This happens as we have multiplied the&nbsp;MID function&nbsp;with 1.<\/li><li>IFERROR((MID(A2,ROW(INDIRECT(\u201c1:\u201d&amp;LEN(A2))),1)*1),\u201d\u201d) \u2013 When IFERROR function is used, it would remove all the #VALUE! errors and only the numbers would remain. The output of this part would look like this \u2013&nbsp;{\u201c\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;\u201d\u201d;1;0;0}<\/li><li>=TEXTJOIN(\u201c\u201d,TRUE,IFERROR((MID(A2,ROW(INDIRECT(\u201c1:\u201d&amp;LEN(A2))),1)*1),\u201d\u201d)) \u2013 The TEXTJOIN function now simply combines the string characters that remains (which are the numbers only) and ignores the empty string.<\/li><\/ul>\n\n\n\n<p><strong>Pro Tip:<\/strong>&nbsp;If you want to check the output of a part of the formula, select the cell, press F2 to get into the edit mode, select the part of the formula for which you want the output and press F9. You will instantly see the result. And then remember to either press Control + Z or hit the Escape key. DO NOT hit the enter key.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/www.dropbox.com\/s\/gzscwa8a8biqwr5\/Get-Numeric-or-Text-Part-from-a-String-Using-Excel-Formulas.xlsm?dl=1\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/v1.nitrocdn.com\/zciqOWZgDznkuqFpvIJsLUeyHlJtlHxe\/assets\/static\/source\/rev-029371b\/wp-content\/uploads\/2015\/09\/Download-File.png\" alt=\"\" class=\"wp-image-11460\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Download the Example File<\/strong><\/p>\n\n\n\n<p>You can also use the same logic to extract the text part from an alphanumeric string. Below is the formula that would get the text part from the string:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">=TEXTJOIN(\"\",TRUE,IF(ISERROR(MID(A2,ROW(INDIRECT(\"1:\"&amp;LEN(A2))),1)*1),MID(A2,ROW(INDIRECT(\"1:\"&amp;LEN(A2))),1),\"\"))<\/pre>\n\n\n\n<p>A minor change in this formula is that&nbsp;IF function&nbsp;is used to check if the&nbsp;array&nbsp;we get from MID function are errors or not. If it\u2019s an error, it keeps the value else it&nbsp;replaces it with a blank.<\/p>\n\n\n\n<p>Then TEXTJOIN is used to combine all the text characters.<strong>Caution<\/strong>: While this formula works great, it uses a volatile function (the INDIRECT function). This means that in case you use this with a huge dataset, it may take some time to give you the results. It\u2019s best to create a backup before you use this formula in Excel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Extract Numbers from String in Excel (for Excel 2013\/2010\/2007)<\/h2>\n\n\n\n<p>If you have Excel 2013. 2010. or 2007, you can not use the TEXTJOIN formula, so you will have to use a complicated formula to get this done.<\/p>\n\n\n\n<p>Suppose you have a dataset as shown below and you want to extract all the numbers in the string in each cell.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/v1.nitrocdn.com\/zciqOWZgDznkuqFpvIJsLUeyHlJtlHxe\/assets\/static\/optimized\/rev-029371b\/wp-content\/uploads\/2017\/08\/Extract-Number-from-String-in-Excel-Data.png\" alt=\"Extract Number from String in Excel - Data\" class=\"wp-image-29591\"\/><\/figure><\/div>\n\n\n\n<p>The below formula will get this done:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">=IF(SUM(LEN(A2)-LEN(SUBSTITUTE(A2, {\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"}, \"\")))&gt;0, SUMPRODUCT(MID(0&amp;A2, LARGE(INDEX(ISNUMBER(--MID(A2,ROW(INDIRECT(\"$1:$\"&amp;LEN(A2))),1))* ROW(INDIRECT(\"$1:$\"&amp;LEN(A2))),0), ROW(INDIRECT(\"$1:$\"&amp;LEN(A2))))+1,1) * 10^ROW(INDIRECT(\"$1:$\"&amp;LEN(A2)))\/10),\"\")<\/pre>\n\n\n\n<p>In case there is no number in the text string, this formula would return blank (empty string).<\/p>\n\n\n\n<p>Although this is an array formula, you&nbsp;<strong>don\u2019t need<\/strong>&nbsp;to use \u2018Control-Shift-Enter\u2019 to use this. A simple enter works for this formula.<\/p>\n\n\n\n<p>Again, this formula will extract all the numbers in the string no matter the position. For example, if the text is \u201cThe price of 10 tickets is USD 200\u201d, it will give you 10200 as the result.<strong>Caution<\/strong>: While this formula works great, it uses a volatile function (the INDIRECT function). This means that in case you use this with a huge dataset, it may take some time to give you the results. It\u2019s best to create a backup before you use this formula in Excel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Separate Text and Numbers in Excel Using VBA<\/h2>\n\n\n\n<p>If separating text and numbers (or extracting numbers from the text) is something you have to often, you can also use the VBA method.<\/p>\n\n\n\n<p>All you need to do is use a simple VBA code to&nbsp;create a custom User Defined Function (UDF) in Excel, and then instead of using long and complicated formulas, use that VBA formula.<\/p>\n\n\n\n<p>Let me show you how to create two formulas in VBA \u2013 one to extract numbers and one to extract text from a string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Extract Numbers from String in Excel (using VBA)<\/h3>\n\n\n\n<p>In this part, I will show you how to create the custom function to get only the numeric part from a string.<\/p>\n\n\n\n<p>Below is the VBA code we will use to create this custom function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Function GetNumeric(CellRef As String)\nDim StringLength As Integer\nStringLength = Len(CellRef)\nFor i = 1 To StringLength\nIf IsNumeric(Mid(CellRef, i, 1)) Then Result = Result &amp; Mid(CellRef, i, 1)\nNext i\nGetNumeric = Result\nEnd Function<\/pre>\n\n\n\n<p>Here are the steps to create this function and then use it in the worksheet:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Go to the Developer tab.<\/li><li>Click on Visual Basic (You can also use the&nbsp;keyboard shortcut&nbsp;ALT + F11)<\/li><li>In the VB Editor backend that opens, right-click on any of the workbook objects.<\/li><li>Go to Insert and click on Module. This will insert the module object for the workbook.<\/li><li>In the Module code window, copy and paste the VBA code mentioned above.<\/li><li>Close the VB Editor.<\/li><\/ul>\n\n\n\n<p>Now, you will be able to use the GetText function in the worksheet. Since we have done all the heavy lifting in the code itself, all you need to do is use the formula&nbsp;=GetNumeric(A2).&nbsp;<\/p>\n\n\n\n<p>This will instantly give you only the numeric part of the string.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/v1.nitrocdn.com\/zciqOWZgDznkuqFpvIJsLUeyHlJtlHxe\/assets\/static\/optimized\/rev-029371b\/wp-content\/uploads\/2017\/08\/Using-Custom-VBA-Function-to-get-only-the-numeric-part-from-a-string-in-Excel.jpg\" alt=\"Using Custom VBA Function to get only the numeric part from a string in Excel\" class=\"wp-image-22859\"\/><\/figure><\/div>\n\n\n\n<p>Note that since the workbook now has VBA code in it, you need to save it with .xls or .xlsm extension.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/www.dropbox.com\/s\/gzscwa8a8biqwr5\/Get-Numeric-or-Text-Part-from-a-String-Using-Excel-Formulas.xlsm?dl=1\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/v1.nitrocdn.com\/zciqOWZgDznkuqFpvIJsLUeyHlJtlHxe\/assets\/static\/source\/rev-029371b\/wp-content\/uploads\/2015\/09\/Download-File.png\" alt=\"\" class=\"wp-image-11460\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Download the Example File<\/strong><\/p>\n\n\n\n<p>In case you have to use this formula often, you can also save this to your&nbsp;Personal Macro Workbook. This will allow you to use this custom formula in any of the Excel workbooks that you work with.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Extract Text from a String in Excel (using VBA)<\/h3>\n\n\n\n<p>In this part, I will show you how to create the custom function to get only the text part from a string.<\/p>\n\n\n\n<p>Below is the VBA code we will use to create this custom function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Function GetText(CellRef As String)\nDim StringLength As Integer\nStringLength = Len(CellRef)\nFor i = 1 To StringLength\nIf Not (IsNumeric(Mid(CellRef, i, 1))) Then Result = Result &amp; Mid(CellRef, i, 1)\nNext i\nGetText = Result\nEnd Function<\/pre>\n\n\n\n<p>Here are the steps to create this function and then use it in the worksheet:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Go to the Developer tab.<\/li><li>Click on Visual Basic (You can also use the&nbsp;keyboard shortcut&nbsp;ALT + F11)<\/li><li>In the VB Editor backend that opens, right-click on any of the workbook objects.<\/li><li>Go to Insert and click on Module. This will insert the module object for the workbook.<ul><li>If you already have a module, double-click on it (no need to insert a new one if you already have it).<\/li><\/ul><\/li><li>In the Module code window, copy and paste the VBA code mentioned above.<\/li><li>Close the VB Editor.<\/li><\/ul>\n\n\n\n<p>Now, you will be able to use the GetNumeric function in the worksheet. Since we have done all the heavy lifting in the code itself, all you need to do is use the formula&nbsp;=GetText(A2).<\/p>\n\n\n\n<p>This will instantly give you only the numeric part of the string.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/v1.nitrocdn.com\/zciqOWZgDznkuqFpvIJsLUeyHlJtlHxe\/assets\/static\/optimized\/rev-029371b\/wp-content\/uploads\/2017\/08\/Using-Custom-VBA-Function-to-get-only-the-Text-part-from-a-string-in-Excel.jpg\" alt=\"Using Custom VBA Function to get only the Text part from a string in Excel\" class=\"wp-image-22862\"\/><\/figure><\/div>\n\n\n\n<p>Note that since the workbook now has VBA code in it, you need to save it with .xls or .xlsm extension.<\/p>\n\n\n\n<p>In case you have to use this formula often, you can also save this to your\u00a0Personal Macro Workbook. This will allow you to use this custom formula in any of the Excel workbooks that you work with.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By&nbsp;Sumit Bansal There is no inbuilt function in Excel to extract the numbers from a string in a cell (or vice versa \u2013 \u00a0remove the numeric part and extract the text part from an alphanumeric string). However, this can be done using a cocktail of&nbsp;Excel functions&nbsp;or some simple&nbsp;VBA code. Let me first show you what <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3210\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,18],"tags":[672,671,673],"class_list":["post-3210","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-microsoft-office","tag-extract-numbers-from-a-cell","tag-extract-numbers-from-a-string-in-excel","tag-extract-numbers-from-a-text"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3210"}],"version-history":[{"count":2,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3210\/revisions"}],"predecessor-version":[{"id":3215,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3210\/revisions\/3215"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}