Data Import & Export

How to Change Excel Column Order When Importing Delimited Text Using VBA

Elise WilliamsElise Williams Nov 2, 2026 869 views

Question details

The user needs to import a delimited text file into Excel and automatically rearrange the column order to match an existing worksheet layout using a macro.

Change Excel Column Order When Importing Delimited Text Using VBA
Product
Excel
Device & OS
not provided
Scenario
Importing external data from a delimited text file where the source column structure differs from the desired destination spreadsheet template.
Observed behavior
The user wants to programmatically map imported text fields to specific Excel columns via VBA rather than manually moving columns after the import is complete.
Before you start

Ensure your delimited text file has consistent field positions or clear header names, and always back up your destination worksheet before running any new VBA macros.

Solution 1Recommended

Use a VBA Macro to Read, Map, and Insert Delimited Text

Create a VBA macro that reads the text file line by line, splits the delimited data into an array, and assigns specific array elements to the desired Excel columns.

Instead of importing the entire file and moving columns afterward, you can use VBA's File I/O capabilities. By reading the text file directly, you can control exactly where each piece of data goes on your worksheet based on its index.

1
Open the VBA Editor

Press 'Alt + F11' in your spreadsheet to open the VBA Editor, then go to 'Insert' > 'Module' to create a new script area.

2
Initialize the File I/O Process

Write a subroutine and use the 'Open' statement to read your text file. For example: `Open "C:\path\to\yourfile.txt" For Input As #1`.

3
Loop and Split the Data

Use a `Do While Not EOF(1)` loop to read each line with `Line Input #1, textLine`. Then, use the `Split(textLine, ",")` function (or your specific delimiter) to break the line into a zero-based data array.

4
Map Array Indices to Destination Columns

Assign the array values to specific cells in your desired order. For example, if you want the third text field (index 2) in the first Excel column, use: `Cells(rowNum, 1).Value = dataArray(2)`.

5
Close the File and Run

Finish the loop, close the file using `Close #1`, and run the macro to instantly import and arrange your data.

Use a VBA Macro to Read, Map, and Insert Delimited Text
Dynamic Header Mapping: If column positions frequently change but header names remain the same, you can program the macro to read the first row, find the array index of each specific header name, and use those dynamic indices to route the data.
Advanced Data Handling in WPS

Import and Manage Data Efficiently with WPS Spreadsheet

WPS Spreadsheet offers comprehensive support for VBA macros, allowing you to seamlessly automate complex data imports, including rearranging columns from delimited text files without manual effort.

  1. 1. Access the Developer Tools: Open WPS Spreadsheet, navigate to the 'Developer' tab, and click 'Visual Basic' to launch the VBA Editor.
  2. 2. Insert Your Macro: Paste your custom text import and column-mapping VBA code into a new module.
  3. 3. Execute the Import: Run the script to automatically extract your delimited data and place it perfectly into your pre-designed worksheet columns.
Fully compatible with Microsoft Excel VBA macros and .xlsm filesBuilt-in Text Import Wizard for users who prefer visual data mappingLightweight and fast performance when processing large text filesCompletely free with a familiar, easy-to-navigate interface
microsoft office alternative - wps office

Frequently Asked Questions

Can I rearrange columns without using VBA?

Yes. You can use the standard Data > From Text/CSV feature or the Text Import Wizard to bring the data into your spreadsheet normally. Once imported, you can manually cut and insert columns to match your desired layout, or use Power Query to reorganize columns before loading them into the worksheet.

What if my delimited text file uses tabs or semicolons instead of commas?

You can easily adapt your VBA script by changing the delimiter character in the Split function. For a tab-separated file, use `Split(textLine, vbTab)`. For semicolons, use `Split(textLine, ";")`.

How do I map columns by header name instead of position in VBA?

You can read the first line of the text file separately before entering the main data loop. Use the Split function on this header line, find the index of the specific header strings (e.g., 'CustomerName' might be index 3), and store that index in a variable to use for mapping in the subsequent data rows.

Does WPS Spreadsheet support running these custom VBA macros?

Yes, WPS Office provides excellent support for VBA macros. Scripts written to read text files and manipulate worksheet cells in Excel will generally execute perfectly in WPS Spreadsheet.