VBA & Macro Problems

How to Trim Text and Round Numbers in Excel using VBA

Aamir Naveed AkramAamir Naveed Akram Oct 16, 2026 870 views

Question details

The user needs a VBA macro to trim text strings and round numeric values within specific Excel column ranges, intentionally skipping column D and avoiding unnecessary loops through empty cells.

How to Trim Text and Round Numbers in Excel using VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating data cleanup across non-contiguous columns while preserving specific text formatting, such as leading zeros, in designated skipped columns.
Observed behavior
Data formatting requires automation where numeric values are rounded to two decimal places and text strings are trimmed of extra spaces without causing application performance issues.
Before you start

Before running the macro, ensure you have saved a backup copy of your workbook, as VBA macro actions typically cannot be undone using the standard Excel Undo feature.

Solution 1Recommended

Use a Union Range VBA Macro

Apply a custom VBA macro utilizing the Union method to target specific non-contiguous ranges, efficiently processing both text and numeric values.

This solution uses the Union function to specify exact columns (like B:C and E:Q), effectively skipping column D to preserve its data format. It also determines the last used row to prevent the macro from freezing or running indefinitely by looping through blank cells.

1
Open the VBA Editor

Press ALT + F11 in Excel to open the Visual Basic for Applications (VBA) window.

2
Insert a New Module

In the Project Explorer panel on the left, right-click on your workbook name, select 'Insert', and click 'Module'.

3
Paste the VBA Code

Copy and paste your custom macro code into the blank module window. Ensure the Union range matches your exact target columns and starting rows (e.g., B26:C and E26:Q).

4
Run the Macro

Close the VBA editor to return to your worksheet. Press ALT + F8 to open the Macro dialog, select your macro name from the list, and click 'Run'.

Use a Union Range VBA Macro
Code Customization: Remember to replace 'Sheet1' in the code with your actual worksheet tab name, and adjust the number in the Round() function if you need a different number of decimal places.
Advanced VBA Support

Easily Run VBA Macros with WPS Spreadsheet

WPS Spreadsheet provides excellent built-in support for VBA macros, allowing you to automate data cleaning tasks like trimming text and rounding numbers with ease. It offers a familiar developer interface that makes transitioning your existing Excel macros straightforward and efficient.

  1. 1. Open the Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet file.
  2. 2. Access Developer Tools: Click on the 'Developer' tab located on the top navigation ribbon.
  3. 3. Open the VBA Editor: Click the 'VBA Editor' button to open the built-in coding environment.
  4. 4. Execute the Code: Paste your data trimming and rounding macro into a module, then click the 'Run' icon to process your selected data ranges immediately.
Fully compatible with Microsoft Excel macro-enabled file formats (.xlsm).Built-in VBA Developer tools to write, edit, and debug your data cleaning scripts.Lightweight software architecture that processes large data ranges quickly.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA code freeze Excel when running?

This usually happens if the macro is looping through an entire column (over 1 million rows) instead of just the used data range. Ensure your code dynamically finds the last row using the End(xlUp) method to limit the processed area.

How can I modify the VBA macro to skip a different column?

You can adjust the Union range parameters in the code. For example, if you want to skip column E instead of D, change the range strings inside the Union function to encapsulate the columns before and after E.

Why do my leading zeros disappear in some columns?

Leading zeros are often dropped when Excel evaluates a cell as a standard number. By intentionally excluding that specific column from the macro's processing range, the cell retains its original text formatting and preserves the leading zeros.