VBA & Macro Problems

How to Split Comma-Separated Numbers into Designated Excel Columns

John WilsonJohn Wilson Oct 22, 2026 870 views

Question details

The user wants to distribute comma-separated numbers from a single cell into specific columns matching their numerical value, rather than filling adjacent columns sequentially.

How to Split Comma-Separated Numbers into Designated Excel Columns
Product
Excel
Device & OS
not provided
Scenario
Organizing comma-separated data strings into structured column positions based on the actual number values.
Observed behavior
Numbers are dynamically separated into exact column indices matching their values (e.g., the number 7 is placed in the 7th column relative to the selected cell).
Before you start

Make sure you have inserted enough empty columns to the right of your selected data range so that the macro does not overwrite your existing data during the split process.

Solution 1Recommended

Use a VBA Macro to Split and Distribute Numbers by Value

Since Excel's default 'Text to Columns' feature only splits data into sequential adjacent columns, you need a custom VBA macro to read the numerical value and offset the placement accordingly.

This VBA script splits the cell contents by commas, trims any extra spaces, and places each number in the corresponding column relative to the selected cell. For instance, if a cell contains '3, 7', the script places 3 in the 3rd column and 7 in the 7th column.

1
Enable the Developer Tab

Select File > Options. Choose Customize Ribbon on the left panel. In the list of Main Tabs on the right, check the Developer box if it is not already selected, and click OK.

2
Open the Visual Basic Editor

Go to the Developer tab and click Visual Basic, or simply press Alt + F11 on your keyboard.

3
Insert a New Module

In the VBA editor, click Insert > Module from the top menu to create a blank window for your code.

4
Paste the VBA Code

Copy and paste the following code into the module window: Sub SplitData() Dim cel As Range Dim arr() As String Dim i As Long Dim c As Long Application.ScreenUpdating = False For Each cel In Selection arr = Split(cel.Value, ",") cel.ClearContents For i = 0 To UBound(arr) c = Val(Trim(arr(i))) If c > 0 Then cel.Offset(0, c - 1).Value = c End If Next i Next cel Application.ScreenUpdating = True End Sub

5
Run the Macro on Your Data

Switch back to your Excel worksheet. Select the range of cells containing the comma-separated numbers you want to split. Navigate to the Developer tab, click Macros (or press Alt + F8), select 'SplitData' from the list, and click Run.

Use a VBA Macro to Split and Distribute Numbers by Value
Save as Macro-Enabled Workbook: To keep this macro for future use, you must save your Excel file as an Excel Macro-Enabled Workbook (*.xlsm).
Advanced Data Processing

Run Macros and Process Data Effortlessly in WPS Spreadsheet

WPS Spreadsheet provides comprehensive support for VBA and macros, allowing you to easily run customized scripts like the SplitData macro. It handles complex data processing tasks while offering a highly intuitive interface.

  1. 1. Open your Workbook in WPS: Launch WPS Spreadsheet and open the document containing your comma-separated data.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click on VBA Editor.
  3. 3. Insert the Code: Click Insert > Module, paste the SplitData VBA code, and close the editor.
  4. 4. Execute the Macro: Select the cells you want to split, click Macros under the Developer tab, select SplitData, and hit Run.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .xlsm)Built-in robust support for VBA macros and customized scriptingAdvanced built-in tools for data cleaning and text-to-columnsLightweight, fast execution, and completely free to download
microsoft office alternative - wps office

Frequently Asked Questions

Can I use the default Text to Columns feature for this?

The default 'Text to Columns' feature under the Data tab can split comma-separated values, but it will only place the numbers into sequential, adjacent columns (1st, 2nd, 3rd column). It cannot read the numerical value to distribute it into a specifically designated column index, which is why a VBA macro is required.

Why am I getting an error when trying to save my workbook after adding the macro?

Standard Excel workbooks (.xlsx) cannot store VBA code. When saving a file that contains macros, you must select 'Excel Macro-Enabled Workbook (*.xlsm)' from the 'Save as type' dropdown menu in the save dialog.

What happens if a cell contains a number larger than the available empty columns?

If the VBA macro tries to place a value in a column index that exceeds the boundaries of your worksheet or overwrites populated cells, you may encounter a runtime error or accidentally delete data. Always ensure sufficient blank columns exist to the right before running the macro.