{"id":373,"date":"2013-02-28T12:42:47","date_gmt":"2013-02-28T12:42:47","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=373"},"modified":"2020-04-23T11:02:10","modified_gmt":"2020-04-23T11:02:10","slug":"excel-vba-lesson-8-string-functions","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-8-string-functions\/","title":{"rendered":"Excel VBA Lesson 8: String Manipulation Functions"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-7-trigonometric-functions\/\">&lt;&lt;Lesson 7&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-9\/\">&lt;&lt;Lesson 9&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<p>Excel VBA can handle strings just as well as the stand-alone Visual Basic program. All the string handling functions in Visual Basic such as Len, Right, Left, Mid, Trim, Ltrim, Rtrim, Ucase, Lcase, Instr, Val, Str ,Chr and Asc can be used in Excel VBA.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.1 The InStr function<\/strong><\/h3>\n\n\n\n<p>InStr is a function that looks for and returns the position of a substring in a phrase<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 8.1<\/strong><\/h4>\n\n\n\n<p>Private Sub cmdInstr_Click()<br>Dim phrase As String<br>phrase = Cells(1, 1).Value<br>Cells(4, 1) = InStr(phrase, &#8220;ual&#8221;)<br>End Sub<\/p>\n\n\n\n<p>The function InStr(phrase,&#8221;ual&#8221;) will find the substring &#8220;ual&#8221; from the phrase &#8220;Visual Basic&#8221; entered in cells(1,1) and then return its position, in this case, it is 4 from the left.<\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.2 The Left function<\/strong><\/h3>\n\n\n\n<p>Left is a function that extracts the characters from a phrase, starting from the left.<\/p>\n\n\n\n<p>Left(phrase,4) means 4 characters are extracted from the phrase, starting from the leftmost position.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 8.2<\/strong><\/h4>\n\n\n\n<p>Private Sub cmdLeft_Click()<br>Dim phrase As String<br>phrase = Cells(1, 1).Value<br>Cells(2, 1) = Left(phrase, 4)<br>End Sub<\/p>\n\n\n\n<p>This code returns the substring &#8220;Visu&#8221; from the phrase &#8220;Visual Basic&#8221; entered in cells(1,1)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.3 The Right function<\/strong><\/h3>\n\n\n\n<p>he Right function that extracts the characters from a phrase, starting from the Right.Right(phrase,5) means 5 characters are extracted from the phrase, starting from the rightmost position.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 8.3<\/strong><\/h4>\n\n\n\n<p>Private Sub cmdRight_Click()<br>Dim phrase As String<br>phrase = Cells(1, 1).Value<br>Cells(3, 1) = Right(phrase, 5)<\/p>\n\n\n\n<p>This code returns the substring &#8220;Basic&#8221; from the phrase &#8220;Visual Basic&#8221; entered in cells(1,1)<\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.4 The Mid function<\/strong><\/h3>\n\n\n\n<p>Mid is a function that extracts a substring from a phrase, starting from the position specified by the second parameter in the bracket.<\/p>\n\n\n\n<p>Mid(phrase,8,3) means a substring of three characters are extracted from the phrase, starting from the 8th position from the left, including empty space.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 8.4<\/strong><\/h4>\n\n\n\n<p>Private Sub cmdMid_Click()<br>Dim phrase As String<br>phrase = Cells(1, 1).Value<br>Cells(5, 1) = Mid(phrase, 8, 3)<br>End Sub<\/p>\n\n\n\n<p>This code returns the substring &#8220;Bas&#8221; from the phrase &#8220;Visual Basic&#8221; entered in cells(1,1)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.5 The Len function<\/strong><\/h3>\n\n\n\n<p>Len is a function that returns the length of a phrase(including empty space in between)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 8.5<\/strong><\/h4>\n\n\n\n<p>Private Sub cmdLen_Click()<br>Dim phrase As String<br>phrase = Cells(1, 1).Value<br>Cells(6, 1) = Len(phrase)<br>End Sub<\/p>\n\n\n\n<p>The code returns 12 for the phrase &#8220;Visual Basic&#8221; entered in cells(1,1)<\/p>\n\n\n\n<p>Visual Basic Editor in MS Excel can handle strings just as good as a stand-alone VB program. All the string handling functions in Visual Basic such as Left, Right, Instr, Mid and Len can be used in Excel Visual Basic Editor.<\/p>\n\n\n\n<p>The output of all the examples are shown in the Figure below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif\" alt=\"Excel VBA\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.6 The Ucase and the Lcase functions<\/strong><\/h3>\n\n\n\n<p>The Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters. For example,<\/p>\n\n\n\n<p>Ucase(\u201cexcel vba\u201d) =EXCEL VBA<\/p>\n\n\n\n<p>Lcase(\u201cExcel VBA\u201d) =excel vba<\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.7 The Str and Val functions<\/strong><\/h3>\n\n\n\n<p>The Str is the function that converts a number to a string while the Val function converts a string to a number. The two functions are important when we need to perform mathematical operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.8 The Chr and the Asc functions<\/strong><\/h3>\n\n\n\n<p>The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for \u201cAmerican Standard Code for Information Interchange\u201d. Altogether there are 255 ASCII codes and as many ASCII characters. Some of the characters may not be displayed as they may represent some actions such as the pressing of a key or produce a beep sound. The format of the Chr function is<\/p>\n\n\n\n<p>Chr(charcode)<\/p>\n\n\n\n<p>and the format of the Asc function is<\/p>\n\n\n\n<p>Asc(Character)<\/p>\n\n\n\n<p>The following are some examples:<\/p>\n\n\n\n<p>Chr(65)=A, Chr(122)=z, Chr(37)=% , Asc(\u201cB\u201d)=66, Asc(\u201c&amp;\u201d)=38<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-7\/\">&lt;&lt;Lesson 7&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-9\/\">&lt;&lt;Lesson 9&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 7&gt;&gt; [Contents] &lt;&lt;Lesson 9&gt;&gt; Excel VBA can handle strings just as well as the stand-alone Visual Basic program. All the string handling functions in Visual Basic such as Len, Right, Left, Mid, Trim, Ltrim, Rtrim, Ucase, Lcase, Instr, Val, Str ,Chr and Asc can be used in Excel VBA. 8.1 The InStr function InStr &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-8-string-functions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 8: String Manipulation Functions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5012,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-373","page","type-page","status-publish","hentry","category-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Lesson 8: String Manipulation Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson illustrates the use of various string manipulation functions in Excel VBA\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/excelvbatutor.com\/vba_lesson8.htm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Lesson 8: String Manipulation Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson illustrates the use of various string manipulation functions in Excel VBA\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson8.htm\" \/>\n<meta property=\"og:site_name\" content=\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-23T11:02:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-8-string-functions\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson8.htm\",\"name\":\"Excel VBA Lesson 8: String Manipulation Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson8.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson8.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif\",\"datePublished\":\"2013-02-28T12:42:47+00:00\",\"dateModified\":\"2020-04-23T11:02:10+00:00\",\"description\":\"This Excel VBA lesson illustrates the use of various string manipulation functions in Excel VBA\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson8.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson8.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson8.htm#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson8.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 8: String Manipulation Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/excelvbatutor.com\/#website\",\"url\":\"https:\/\/excelvbatutor.com\/\",\"name\":\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"description\":\"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Lesson 8: String Manipulation Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson illustrates the use of various string manipulation functions in Excel VBA","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/excelvbatutor.com\/vba_lesson8.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 8: String Manipulation Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson illustrates the use of various string manipulation functions in Excel VBA","og_url":"https:\/\/excelvbatutor.com\/vba_lesson8.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T11:02:10+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-8-string-functions\/","url":"https:\/\/excelvbatutor.com\/vba_lesson8.htm","name":"Excel VBA Lesson 8: String Manipulation Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson8.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson8.htm#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif","datePublished":"2013-02-28T12:42:47+00:00","dateModified":"2020-04-23T11:02:10+00:00","description":"This Excel VBA lesson illustrates the use of various string manipulation functions in Excel VBA","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson8.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson8.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson8.htm#primaryimage","url":"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif","contentUrl":"https:\/\/www.vbtutor.net\/Images\/progra_jan_29_2008_vba9.gif"},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson8.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 8: String Manipulation Functions"}]},{"@type":"WebSite","@id":"https:\/\/excelvbatutor.com\/#website","url":"https:\/\/excelvbatutor.com\/","name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/users\/5012"}],"replies":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/comments?post=373"}],"version-history":[{"count":25,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/373\/revisions"}],"predecessor-version":[{"id":3342,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/373\/revisions\/3342"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}