{"id":30173,"date":"2023-07-31T18:48:05","date_gmt":"2023-07-31T16:48:05","guid":{"rendered":"https:\/\/www.myexcelonline.com\/?p=30173"},"modified":"2024-03-30T21:11:25","modified_gmt":"2024-03-30T20:11:25","slug":"vba-replace-function","status":"publish","type":"post","link":"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/vba-replace-function\/","title":{"rendered":"How to Use VBA REPLACE Function in Excel &#8211; A Guide to Text Manipulation with 4 Examples"},"content":{"rendered":"<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/B83C3F94-6336-4E4D-88E0-B7241970031A.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-30241\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/B83C3F94-6336-4E4D-88E0-B7241970031A-1024x576.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"1024\" height=\"576\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/B83C3F94-6336-4E4D-88E0-B7241970031A-1024x576.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/B83C3F94-6336-4E4D-88E0-B7241970031A-300x169.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/B83C3F94-6336-4E4D-88E0-B7241970031A-768x432.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/B83C3F94-6336-4E4D-88E0-B7241970031A.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>Excel <a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/free-excel-macros-and-vba-training-2021\/\" target=\"_blank\"><strong>VBA<\/strong><\/a> allows users to <strong>automate tasks<\/strong>, <strong>perform complex calculations, and manipulate data efficiently<\/strong>. Among a plethora of functions available in Excel <a class=\"wpil_keyword_link\" title=\"VBA\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/101-excel-macros-examples\/\" target=\"_blank\" rel=\"noopener\" data-wpil-keyword-link=\"linked\">VBA<\/a>, <strong>VBA Replace Function stands out as a pioneering tool in text manipulation<\/strong>.<\/p>\n<p>In this article, we will explore the following topics &#8211;<\/p>\n<p>Download the Excel Workbook below to follow along and understand how to use VBA Replace Function in Excel \u2013<br \/>\n<a class=\"IRPP_minimalist icon-download\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace.xlsm\" target=\"_blank\"><span class=\"ctaText px-3 text-capitalize\"><strong>download excel workbook<\/strong>\u00a0<\/span><span class=\"postTitle px-3\">VBA-Replace.xlsm<\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><strong><span id=\"1\" style=\"color: #ff0000\">Introduction to VBA Replace<\/span><\/strong><\/h3>\n<p>The VBA Replace function is a powerful tool used to manipulate <a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/how-to-use-substrings-in-excel\/\" target=\"_blank\"><strong>text strings<\/strong><\/a> in Microsoft Excel. It is used to <strong>replace a set of characters from a text string with a new set of characters<\/strong>. The <strong>syntax<\/strong> of VBA Replace is &#8211;<\/p>\n<p><strong><span style=\"color: #ff0000\">Replace(Expression, Find, ReplaceWith,\u00a0[\u00a0<em>start<\/em>, [\u00a0<em>count<\/em>, [\u00a0<em>compare<\/em>\u00a0]]])<\/span><\/strong><\/p>\n<p>where,<\/p>\n<ul>\n<li><strong>Expression &#8211;<\/strong>\u00a0The original text string where you want to perform the replacement.<\/li>\n<li><strong>Find &#8211;<\/strong>\u00a0The substring you want to find and replace within the text string.<\/li>\n<li><strong>ReplaceWith &#8211;<\/strong>\u00a0The string that will replace the original substring.<\/li>\n<li><strong>Start <em>(optional)<\/em> &#8211;<\/strong> The position in the Expression from where the search should begin. By default, it starts from the first character (position 1).<\/li>\n<li><strong>Count <em>(optional)<\/em> &#8211;<\/strong> The number of occurrences to replace. If omitted, all occurrences will be replaced.<\/li>\n<li><strong>Compare <em>(optional) &#8211;<\/em><\/strong>\u00a0Specifies the type of string comparison. There are three options &#8211;\n<ul>\n<li>vbBinaryCompare (case-sensitive). <em>Default<\/em><\/li>\n<li>vbTextCompare (case-insensitive)<\/li>\n<li>vbDatabaseCompare (used for database comparisons).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><strong><span id=\"2\" style=\"color: #ff0000\">Example 1 &#8211; Simple Replacement<\/span><\/strong><\/h3>\n<p>In this example, we will use VBA Replace to perform a simple text substitution. Our goal is to <strong>substitute the text &#8220;v4.0&#8221; with &#8220;v5.0&#8221;<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-1.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30175 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-1.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"617\" height=\"364\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-1.png 617w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-1-300x177.png 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/a><\/p>\n<p>Follow the <strong>steps<\/strong> below to understand how to accomplish this &#8211;<\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 1:<\/span><\/strong> Open the Workbook and press <strong>Alt + F11<\/strong> to open the VBA editor.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-2.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30176\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-2.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"699\" height=\"371\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-2.png 820w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-2-300x159.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-2-768x407.png 768w\" sizes=\"(max-width: 699px) 100vw, 699px\" \/><\/a><\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 2:<\/span><\/strong> Click on <strong>Insert<\/strong> &gt; <strong>Module<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-3.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30177 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-3.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"427\" height=\"323\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-3.png 427w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-3-300x227.png 300w\" sizes=\"(max-width: 427px) 100vw, 427px\" \/><\/a><\/p>\n<p><strong><span style=\"color: #ff0000\">STEP 3:<\/span><\/strong> Write the following <strong>code<\/strong> &#8211;<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-4.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30178 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-4.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"1606\" height=\"423\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-4.png 1606w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-4-300x79.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-4-1024x270.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-4-768x202.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-4-1536x405.png 1536w\" sizes=\"(max-width: 1606px) 100vw, 1606px\" \/><\/a><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Sub REPLACE_Ex1()\r\nRange(\"A5\").Value = Replace(Range(\"A2\"), \"v4.0\", \"v5.0\")\r\nEnd Sub<\/pre>\n<ul>\n<li><strong>Range(&#8220;A2&#8221;) &#8211;<\/strong> Range A2 contains the text string.<\/li>\n<li><strong>v4.0 &#8211;<\/strong> The substring you want to find and replace.<\/li>\n<li><strong>v5.0 &#8211;<\/strong> The string that will replace the original substring (i.e. <strong>v4.0<\/strong>).<\/li>\n<li><strong>Range(&#8220;A5&#8221;).Value<\/strong> &#8211; The replaced text string will be displayed in cell A5.<\/li>\n<\/ul>\n<p><strong><span style=\"color: #ff0000\">STEP 4:<\/span><\/strong> Press the <strong>Run<\/strong> icon.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-5.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30179 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-5.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"1606\" height=\"423\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-5.png 1606w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-5-300x79.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-5-1024x270.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-5-768x202.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-5-1536x405.png 1536w\" sizes=\"(max-width: 1606px) 100vw, 1606px\" \/><\/a><\/p>\n<p>In cell A5, every instance of &#8216;v4.0&#8217; has been successfully substituted with &#8216;v5.0&#8217;.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-6.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30180 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-6.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"621\" height=\"360\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-6.png 621w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-6-300x174.png 300w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><strong><span id=\"3\" style=\"color: #ff0000\">Example 2 &#8211; Case Insensitive Replacement<\/span><\/strong><\/h3>\n<p>VBA Replace is a<strong> case-sensitive function<\/strong> but you can <strong>use the optional argument &#8220;compare&#8221; to make it work in a case-insensitive manner<\/strong>.<\/p>\n<p>In this example, you want to<strong> replace all the occurrences of the word &#8220;John&#8221; with &#8220;Jack&#8221;<\/strong>. But as you can see, some of them are spelled in uppercase, some in lowercase, and some in mixed case.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-7.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30181 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-7.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"607\" height=\"369\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-7.png 607w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-7-300x182.png 300w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/a><\/p>\n<p>If we use the same code as before, only the occurrence &#8220;John&#8221; will be replaced. The ones that are in uppercase or lowercase will stay unchanged.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Sub REPLACE_Ex1()\r\nRange(\"A5\").Value = Replace(Range(\"A2\"), \"John\", \"Jack\")\r\nEnd Sub<\/pre>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-8.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30182\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-8.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"773\" height=\"212\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-8.png 1441w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-8-300x82.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-8-1024x281.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-8-768x211.png 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-9.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30183 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-9.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"608\" height=\"370\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-9.png 608w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-9-300x183.png 300w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/a><\/p>\n<p>Now, let&#8217;s use the <strong>compare<\/strong> argument. It has<strong> three options<\/strong> &#8211; vbBinaryCompare, vbTextCompare, and vbDatabaseCompare. This argument specifies the type of string comparison to be used in the Replace function.<\/p>\n<p>Here, &#8220;<strong>vbTextCompare<\/strong>&#8221; is used, which means the replacement will be<strong> case-insensitive<\/strong>.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Sub REPLACE_Ex1()\r\nRange(\"A5\").Value = Replace(Range(\"A2\"), \"John\", \"Jack\", , ,vbTextCompare)\r\nEnd Sub<\/pre>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-11.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30185 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-11.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"1457\" height=\"423\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-11.png 1457w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-11-300x87.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-11-1024x297.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-11-768x223.png 768w\" sizes=\"(max-width: 1457px) 100vw, 1457px\" \/><\/a><\/p>\n<p>All the occurrences of John have been replaced irrespective of their case.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-10.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30184 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-10.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"611\" height=\"375\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-10.png 611w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-10-300x184.png 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><strong><span id=\"4\" style=\"color: #ff0000\">Example 3 &#8211; Replace using Start Argument<\/span><\/strong><\/h3>\n<p>Suppose you want to <strong>start replacing &#8220;John&#8221; with &#8220;Jack&#8221; from the 2nd position<\/strong> onwards. This can be executed using the <strong>Start<\/strong> argument in the VBA Replace function. The &#8220;Start&#8221; argument in the VBA Replace function<strong> allows you to specify the start position in the text string\u00a0<\/strong>and will l<strong>eave out any character before the start position<\/strong>.<\/p>\n<p>This can be particularly useful when you want to<strong> skip certain occurrences of the substring or start replacing from a specific position<\/strong>.<\/p>\n<p>The code that can be used is &#8211;<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-12.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30189 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-12.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"1608\" height=\"424\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-12.png 1608w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-12-300x79.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-12-1024x270.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-12-768x203.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-12-1536x405.png 1536w\" sizes=\"(max-width: 1608px) 100vw, 1608px\" \/><\/a><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Sub REPLACE_Ex3()\r\nRange(\"A5\").Value = Replace(Range(\"A2\"), \"John\", \"Jack\", 79)\r\nEnd Sub<\/pre>\n<p>The total length of the characters before the 2nd occurrence (&#8220;John is a seasoned investment banker known for his excellent market analysis. &#8220;) is 78. So, the start parameter should be set to 79 since the second occurrence of &#8216;John&#8217; begins at the 79th position.<\/p>\n<p>Excel will <strong>begin searching the <a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/extract-words-from-cell\/\" target=\"_blank\" rel=\"noopener\">text<\/a> string from the 79th position<\/strong> and<strong> replace all occurrences of John with Jack from the 79th position<\/strong>. The characters spanning from the 1st position to the 78th position will be removed from the result.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-13.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30190 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-13.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"617\" height=\"359\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-13.png 617w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-13-300x175.png 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><strong><span id=\"5\" style=\"color: #ff0000\">Example 4 &#8211; Replace using Count Argument<\/span><\/strong><\/h3>\n<p>The Count argument in the VBA Replace function allows you to <strong>specify the number of occurrences of the substring you want to replace within the text string<\/strong>. If omitted, the default value is -1, which means, make all possible substitutions.<\/p>\n<p>This is particularly useful when you want to <strong>limit the number of replacements made, rather than replacing all occurrences<\/strong> in the entire string.<\/p>\n<p>Suppose you want to <strong>replace only 2 occurrences of &#8220;John&#8221; with &#8220;Jack&#8221;<\/strong>. The code that you can use will be &#8211;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Sub REPLACE_Ex4()\r\nRange(\"A5\").Value = Replace(Range(\"A2\"), \"John\", \"Jack\", , 2)\r\nEnd Sub<\/pre>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-14.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30191 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-14.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"1603\" height=\"492\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-14.png 1603w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-14-300x92.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-14-1024x314.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-14-768x236.png 768w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-14-1536x471.png 1536w\" sizes=\"(max-width: 1603px) 100vw, 1603px\" \/><\/a><\/p>\n<p>Only the 1st 2 occurrences of John have been replaced with Jack, and the remaining 2 occurrences remain unchanged.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-15.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-30192 size-full\" title=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-15.png\" alt=\"How to Use VBA REPLACE Function in Excel - A Guide to Text Manipulation with 4 Examples\" width=\"620\" height=\"363\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-15.png 620w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2023\/07\/VBA-Replace-15-300x176.png 300w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>The article discusses Excel VBA and its powerful VBA Replace function for text manipulation. It covers four examples of its usage: simple replacement, case-insensitive replacement, replacement using the Start argument, and replacement using the Count argument, demonstrating how to automate text substitutions in <a class=\"wpil_keyword_link\" title=\"Microsoft Excel\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/free-microsoft-excel-online-course\/\" target=\"_blank\" rel=\"noopener\" data-wpil-keyword-link=\"linked\">Microsoft Excel<\/a> effectively.<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/replace-function\" target=\"_blank\" rel=\"nofollow noopener\"><strong>Click here<\/strong><\/a> to learn more about For Loop in Excel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Excel VBA allows users to automate tasks, perform complex calculations, and manipulate data efficiently. Among a plethora of functions available in Excel VBA, VBA Replace Function stands out as a pioneering tool in text manipulation. In this article, we will explore the following topics &#8211; Download the Excel Workbook below to follow along and understand [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":30241,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cos_headline_score":0,"cos_seo_score":0,"cos_headline_text":"","cos_headline_has_been_analyzed":false,"cos_last_analyzed_headline":[],"om_disable_all_campaigns":false},"categories":[56],"tags":[1308,1309,1307],"acf":[],"_links":{"self":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts\/30173"}],"collection":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/comments?post=30173"}],"version-history":[{"count":0,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts\/30173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/media\/30241"}],"wp:attachment":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/media?parent=30173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/categories?post=30173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/tags?post=30173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}