How to Extract Structured Text into Excel Columns with a Macro
Question details
The user needs to automate the process of extracting consistently formatted text and parsing it into dedicated Excel columns using a VBA macro.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the extraction of text data, such as flight logs or registration records, into an organized tabular layout for analysis.
- Observed behavior
- Text contains mixed separators (slashes, periods, line breaks) that must be normalized and split into an array before being mapped to specific destination columns.
Ensure your source text follows a consistent format and structure, and back up your workbook before running any new macros to prevent unintended data overwrites.
Use a VBA Macro to Normalize Delimiters and Split Text
This method uses VBA functions to replace various separators with spaces, collapse multiple spaces, and distribute the resulting array elements into specific columns.
By concatenating the source lines and normalizing the delimiters, you create a uniform string that VBA can easily parse.
The VBA Split function is then used to break the string into an array, which allows you to programmatically assign each piece of data to columns like Number, Registration, Adult, Child, Infant, Cargo, Mail, and Baggage.
Press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor in Excel.
Click on 'Insert' in the top menu and select 'Module' to create a blank workspace for your macro code.
Write your VBA code using the Replace function to substitute slashes ('/'), periods ('.'), and line breaks (vbCrLf) with a standard space character (' ').
Use a loop or the Application.WorksheetFunction.Trim method to reduce any multiple consecutive spaces down to a single space.
Use the Split function to convert the cleaned text string into an array. Loop through the array elements and assign them to the target worksheet columns.

Extract Text Using Power Query
If you prefer a visual, no-code approach over VBA, Power Query can effectively clean and split structured text into columns.
Effortlessly Manage Macros and Data Extraction in WPS Spreadsheet
WPS Spreadsheet provides comprehensive support for VBA macros, allowing you to seamlessly run text parsing and extraction scripts. Enjoy robust data handling capabilities in a highly compatible and user-friendly environment.
- 1. Open Your Data File: Launch WPS Spreadsheet and open the workbook containing the structured text you need to parse.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic' to open the VBA editor.
- 3. Run the Extraction Macro: Insert your VBA text-splitting code into a module, select the target text cells, and run the macro.
- 4. Save as Macro-Enabled: Save your document as a Macro-Enabled Workbook (.xlsm) to ensure your extraction scripts remain functional for future use.

Frequently Asked Questions
Why is my macro pasting data into the wrong Excel columns?
This generally happens when the input text has inconsistent delimiters or missing fields. If consecutive spaces are not properly collapsed into a single space before splitting, empty array elements will push the data into incorrect columns.
How do I replace line breaks in VBA?
You can replace line breaks by using the VBA Replace function to target standard line break constants, such as vbCrLf (carriage return and line feed), vbCr, or vbLf, and replace them with a space.
Can I use formulas instead of a VBA macro to split this text?
Yes, if you use modern spreadsheet versions, functions like TEXTSPLIT combined with SUBSTITUTE and TRIM can achieve similar results. However, a VBA macro is often more robust for multi-line text blocks with highly mixed delimiters.
Does WPS Office support VBA macros for text manipulation?
Yes, WPS Spreadsheet fully supports VBA macros. You can write, edit, and execute Visual Basic scripts to automate text parsing, delimiter normalization, and data distribution exactly as you would in Microsoft Excel.




