{"id":5627,"date":"2023-04-12T15:19:42","date_gmt":"2023-04-12T15:19:42","guid":{"rendered":"https:\/\/www.bizinfograph.com\/resource\/?p=5627"},"modified":"2026-05-07T12:17:55","modified_gmt":"2026-05-07T12:17:55","slug":"how-to-use-excel-vba-trim-function","status":"publish","type":"post","link":"https:\/\/www.bizinfograph.com\/resource\/how-to-use-excel-vba-trim-function\/","title":{"rendered":"How to use Excel VBA TRIM Function?"},"content":{"rendered":"<p>Excel VBA TRIM Function offers a powerful solution for removing leading and trailing spaces from text strings within Excel macros. Whether you&#8217;re manipulating data, processing user inputs, or formatting text, Excel VBA TRIM Function ensures cleaner and more consistent results. Say goodbye to manual space removal and hello to efficiency with this versatile function. From enhancing data accuracy to improving the performance of your VBA code, Excel VBA TRIM Function streamlines your workflow, saving you time and effort. Take control of your text processing tasks and optimize your Excel macros with the robust capabilities of Excel VBA TRIM Function. With just a simple line of code, you can eliminate extraneous spaces, ensuring that your macros produce accurate and reliable outputs. Embrace the convenience and reliability of Excel VBA TRIM Function and elevate your VBA programming experience to new heights of productivity and accuracy.<\/p>\n<p><strong>This Tutorial Covers:<\/strong><\/p>\n<ol>\n<li><a href=\"#what-is-vba-trim-function\">W<strong>hat is VBA TRIM Function<\/strong><\/a>\n<ul>\n<li>Syntax of the VBA TRIM function<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#why-do-we-also-need-a-trim-function-in-vba\"><strong>Why do we also need a TRIM function in VBA<\/strong><\/a><\/li>\n<li><a href=\"#how-to-use-excel-vba-trim-function\"><strong>How to Use Excel VBA Trim Function<\/strong><\/a>\n<ul>\n<li>VBA Trim Function \u2013 Example 1<\/li>\n<li>VBA Trim Function \u2013 Example 2<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#pros-of-excel-vba-trim-function\"><strong>Pros of Excel VBA Trim Function<\/strong><\/a><\/li>\n<li><a href=\"#things-to-remember\"><strong>Things To Remember<\/strong><\/a><\/li>\n<\/ol>\n<h2><strong id=\"what-is-vba-trim-function\">1. What is VBA TRIM Function?<\/strong><\/h2>\n<p>Excel&#8217;s TRIM string function eliminates spaces from the selected cells. The String and Text methods in VBA include the TRIM function. Worksheet functions are what this VBA procedure is. Similar to the worksheet example, one can trim or eliminate extra spaces from a string using this function. It only requires one argument, a text input, and outputs the result as a string.<\/p>\n<ul>\n<li>\n<h3><strong>Syntax of the VBA TRIM function:<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>The TRIM function has an easy-to-understand syntax:<\/p>\n<p><strong>=TRIM(text)<\/strong><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-5628\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-1.png\" alt=\"Excel VBA TRIM Function\" width=\"203\" height=\"97\" \/><\/p>\n<p>The input, &#8220;text,&#8221; can either be a text string or a variable that contains a text string.<\/p>\n<h2><strong id=\"why-do-we-also-need-a-trim-function-in-vba\">2. Why do we also need a TRIM function in VBA?<\/strong><\/h2>\n<p>In VBA, the TRIM function is used to remove extra spaces from a string. When we input data from different sources like user input, a file, or a database, the data may contain leading or trailing spaces that can cause errors or inconsistencies in the program. We can remove those extra spaces using the TRIM function and make the data more consistent and usable.<\/p>\n<p>While Excel and some other applications may have built-in TRIM functions, it&#8217;s important to have a TRIM function in VBA as it allows us to manipulate string data within VBA code itself. VBA&#8217;s TRIM function allows us to clean up and standardize data in strings within our VBA code without relying on other applications.<\/p>\n<p>Additionally, the TRIM function in VBA can be used with other string functions like LEFT, RIGHT, MID, and CONCATENATE to build more complex expressions and manipulate text data in VBA. Therefore, a TRIM function in VBA can be very useful in data processing and analysis tasks, especially when dealing with large string data sets.<\/p>\n<h2><strong id=\"how-to-use-excel-vba-trim-function\">3. How to Use Excel VBA TRIM Function?<\/strong><\/h2>\n<p>The Trim function in VBA is employed to eliminate excess spaces from a string. We&#8217;ll go over the VBA Trim Function&#8217;s usage by giving some instances. They&#8217;ll be discussed in the following part.<\/p>\n<ul>\n<li>\n<h3><strong>VBA TRIM Function \u2013 Example 1:<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>The example dataset\u00a0is shown in the screenshot that follows.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-5629\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-2.png\" alt=\"Excel VBA TRIM Function\" width=\"347\" height=\"155\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-2.png 347w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-2-300x134.png 300w\" sizes=\"(max-width: 347px) 100vw, 347px\" \/><\/p>\n<p>Each cell in B2, B3, and B4 has a few spaces beside each. The text in cell B2 begins with spaces. There are blank spaces in cell B3. And the text in cell B4\u00a0is separated by blank space.<\/p>\n<p><strong>The steps to use Excel VBA TRIM Function are described below:<\/strong><\/p>\n<p><strong>Step 1:<\/strong> We&#8217;ll use Excel VBA to trim these cells. For that, hit Alt + F11 simultaneously, or select &#8220;Visual Basic&#8221; from the &#8220;Developer&#8221; tab as displayed below.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-5630\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-3.png\" alt=\"Excel VBA TRIM Function\" width=\"642\" height=\"139\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-3.png 642w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-3-300x65.png 300w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/p>\n<p><strong>Step 2:<\/strong> By doing so, the &#8220;Visual Basic&#8221; writing window will open. Now pick &#8220;Module&#8221; from the &#8220;Insert&#8221; menu by right clicking on mouse in the desired sheet name, as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5631\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-4.png\" alt=\"Excel VBA TRIM Function\" width=\"497\" height=\"455\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-4.png 497w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-4-300x275.png 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/p>\n<p><strong>Step 3:<\/strong> This will produce a new Module in which we can put the trimming code. To immediately clean up this data and eliminate all leading and trailing spaces, use the following code:<\/p>\n<p><strong>Sub Excel_Trim_Data()<\/strong><\/p>\n<p><strong>Dim A As Range<\/strong><\/p>\n<p><strong>Set A = Selection<\/strong><\/p>\n<p><strong>For Each cell In A<\/strong><\/p>\n<p><strong>cell.Value = WorksheetFunction.Trim(cell)<\/strong><\/p>\n<p><strong>Next<\/strong><\/p>\n<p><strong>End Sub<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5632\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-5.png\" alt=\"Excel VBA TRIM Function\" width=\"643\" height=\"278\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-5.png 643w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-5-300x130.png 300w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>By doing this, we finish the coding necessary to create a program using VBA Trim.<\/p>\n<p>Let&#8217;s now insert the code that was generated into a tab.<\/p>\n<p>For this, pick &#8220;Shapes&#8221; from the &#8220;Illustrations&#8221; section of the &#8220;Insert&#8221; tab. Create any shape using shape formats and styles from the &#8220;Shapes&#8221; menu choice.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5633\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-6.png\" alt=\"Excel VBA TRIM Function\" width=\"389\" height=\"442\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-6.png 389w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-6-264x300.png 264w\" sizes=\"(max-width: 389px) 100vw, 389px\" \/><\/p>\n<p>Here, as shown below, we have made a rectangular(round corner) box and given it the name &#8220;TRIM&#8221;.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5634\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-7.png\" alt=\"Excel VBA TRIM Function\" width=\"173\" height=\"87\" \/><\/p>\n<p>Now right-click on the newly formed box or shape while still in the \u201cDeveloper\u201d menu and select \u201cDesign Mode\u201d. To connect our created code, choose \u201cAssign Macro\u201d from the right-click menu selection, as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5635\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-8.png\" alt=\"Excel VBA TRIM Function\" width=\"462\" height=\"394\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-8.png 462w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-8-300x256.png 300w\" sizes=\"(max-width: 462px) 100vw, 462px\" \/><\/p>\n<p>When the Assign Marco window appears, pick the previously created macro code\u2014in this case, &#8220;Trim Data&#8221;\u2014and then click OK.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5636\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-9.png\" alt=\"Excel VBA TRIM Function\" width=\"287\" height=\"280\" \/><\/p>\n<p>Select the data and then pick &#8220;TRIM&#8221; from the menu. As shown below, the chosen area will be trimmed, and any unnecessary spaces will be eliminated.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5637\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-10.png\" alt=\"Excel VBA TRIM Function\" width=\"639\" height=\"170\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-10.png 639w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-10-300x80.png 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/p>\n<ul>\n<li>\n<h3><strong>VBA Trim Function \u2013 Example 2:<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>There is another way to write the VBA coding that is shown above. Only the chosen data were trimmed in the aforementioned example. This will also include inserting the function in the message window, which will display once the data has been trimmed.<\/p>\n<p>We will use the same data which we used\u00a0in example 1 for this. Use the code below by following the same procedures. Now analyze the same code which we have written in example 1. However, we will put some text here in order to print a message box with text.<\/p>\n<p><strong>Sub Excel_Trim_Data_With_Message()<\/strong><\/p>\n<p><strong>Dim A As Range<\/strong><\/p>\n<p><strong>Set A = Selection<\/strong><\/p>\n<p><strong>For Each cell In A<\/strong><\/p>\n<p><strong>cell.Value = WorksheetFunction.Trim(cell)<\/strong><\/p>\n<p><strong>Dim B As String<\/strong><\/p>\n<p><strong>B = Trim(&#8220;Trimming is Done&#8221;)<\/strong><\/p>\n<p><strong>MsgBox B<\/strong><\/p>\n<p><strong>Next<\/strong><\/p>\n<p><strong>End Sub<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5638\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-11.png\" alt=\"Excel VBA TRIM Function\" width=\"642\" height=\"277\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-11.png 642w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-11-300x129.png 300w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/p>\n<p>The Visual Basic windows should be closed once we have finished entering the code. Follow the same steps to assign the macro code to the \u201cTRIM\u201d button as shown in example 1.<\/p>\n<p>By doing this, the TRIM Button will be given our trim coding. To test the data, first select it, and then hit the TRIM button.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5639\" src=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-12.png\" alt=\"Excel VBA TRIM Function\" width=\"643\" height=\"206\" srcset=\"https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-12.png 643w, https:\/\/www.bizinfograph.com\/resource\/wp-content\/uploads\/2023\/03\/Excel-VBA-TRIM-Function-12-300x96.png 300w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>The message &#8220;Trimming is Done&#8221; can also be seen here, as can be seen above where our data was trimmed.<\/p>\n<h2><strong id=\"pros-of-excel-vba-trim-function\">4. Pros of Excel VBA Trim Function:<\/strong><\/h2>\n<ul>\n<li>Without counting the number of additional spaces in the cells, we can quickly and efficiently remove enormous amounts of data.<\/li>\n<li>Trimming function coding is also fairly minimal.<\/li>\n<li>The likelihood of any additional room being saved is extremely low or nonexistent.<\/li>\n<\/ul>\n<h2>5. <strong id=\"things-to-remember\">Things To Remember:<\/strong><\/h2>\n<ul>\n<li>Save the work as a Macro-Enabled Worksheet at all times. We can use the assigned macro numerous times by doing this.<\/li>\n<li>Before giving the code to any button, compile it.<\/li>\n<li>There are two distinct methods for making a button, and either one can be used depending on the needs or preferences of the user.<\/li>\n<li>Think about creating VBA code in the manner illustrated in example 2, which will also output the me.<\/li>\n<\/ul>\n<h2>Application of Excel VBA TRIM Function<\/h2>\n<ul>\n<li><strong>Data Cleansing<\/strong>: Utilize the Excel VBA TRIM function to clean leading and trailing spaces from text strings, ensuring data consistency and accuracy.<\/li>\n<li><strong>User Input Validation<\/strong>: Validate user inputs within Excel VBA macros by trimming leading and trailing spaces, preventing data entry errors.<\/li>\n<li><strong>String Comparison<\/strong>: Compare text strings accurately by removing spaces with the Excel VBA TRIM function, facilitating reliable string comparisons.<\/li>\n<li><strong>Text Formatting<\/strong>: Format text output in Excel VBA macros by trimming spaces from generated strings, enhancing readability and presentation.<\/li>\n<li><strong>Database Integration<\/strong>: Prepare text data for integration with databases or other systems by using the Excel VBA TRIM function to remove extraneous spaces.<\/li>\n<li><strong>Dynamic Text Processing<\/strong>: Dynamically process text strings in Excel VBA macros by incorporating the TRIM function, enabling efficient manipulation and analysis of textual data.<\/li>\n<\/ul>\n<p><strong>For ready-to-use Dashboard Templates:<\/strong><\/p>\n<ol>\n<li><a href=\"https:\/\/www.bizinfograph.com\/dashboard-templates#Finance\" target=\"_blank\" rel=\"noopener\">Financial Dashboards<\/a><\/li>\n<li><a href=\"https:\/\/www.bizinfograph.com\/dashboard-templates#Sales\" target=\"_blank\" rel=\"noopener\">Sales Dashboards<\/a><\/li>\n<li><a href=\"https:\/\/www.bizinfograph.com\/dashboard-templates#Human-Resource\" target=\"_blank\" rel=\"noopener\">HR Dashboards<\/a><\/li>\n<li><a href=\"https:\/\/www.executiveknowledge.org\/dashboard-templates\/35\" target=\"_blank\" rel=\"noopener\">Data Visualization Charts<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Excel VBA TRIM Function offers a powerful solution for removing leading and trailing spaces from text strings within Excel macros. Whether you&#8217;re manipulating data, processing user inputs, or formatting text, Excel VBA TRIM Function ensures cleaner and more consistent results. Say goodbye to manual space removal and hello to efficiency with this versatile function. From &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.bizinfograph.com\/resource\/how-to-use-excel-vba-trim-function\/\"> <span class=\"screen-reader-text\">How to use Excel VBA TRIM Function?<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[667],"tags":[668],"class_list":["post-5627","post","type-post","status-publish","format-standard","hentry","category-excel-vba-trim-function","tag-excel-vba-trim-function"],"_links":{"self":[{"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/posts\/5627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/comments?post=5627"}],"version-history":[{"count":5,"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/posts\/5627\/revisions"}],"predecessor-version":[{"id":9234,"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/posts\/5627\/revisions\/9234"}],"wp:attachment":[{"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/media?parent=5627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/categories?post=5627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bizinfograph.com\/resource\/wp-json\/wp\/v2\/tags?post=5627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}