VBA & Macro Problems

Import and Format a TXT File into an Excel Table with VBA

Ayan MasoodAyan Masood Nov 12, 2026 869 views

Question details

The user needs to import a TXT file into an existing Excel table with VBA while handling blank lines, delimited records, and sections with variable lengths.

Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A VBA workflow must let the user choose a text file, read its contents, identify variable-length sections, split records by a delimiter, and write the fields to the RawData worksheet or an existing table.
Observed behavior
The text file contains blank lines and sections with different numbers of lines, so fixed row positions are unreliable when mapping records into the worksheet.
Before you start

Confirm the target worksheet name, table name or starting cell, and the delimiter used by the TXT file. Also identify section labels or markers that VBA can use instead of assuming every section has a fixed number of rows.

Solution 1Recommended

Use VBA to import variable-length TXT sections into the RawData table

Use this approach when the file contains blank lines, delimited records, and sections whose lengths change between files.

Read the file line by line, skip empty records, and use labels or markers to determine the current section. Split each valid record with the actual delimiter, then write the resulting fields to the worksheet or resize the existing table as needed.

The delimiter in the example is a comma. Replace it with a tab, semicolon, pipe, or another separator when the source file uses a different format.

1
Open the VBA editor

In Excel, press Alt+F11, choose Insert > Module, and create a standard module in the workbook that contains the RawData worksheet.

2
Select and read the TXT file

Use Application.GetOpenFilename with a TXT filter, open the selected file for input, and read each line with Line Input into a dynamic string array or collection.

3
Ignore blanks and identify sections

Loop through the lines, skip entries where Trim$(line) is empty, and test each nonblank line for section labels or markers instead of relying on fixed row numbers.

4
Split and write each record

Apply Split(line, delimiter), then write the resulting array to ThisWorkbook.Worksheets("RawData") or to the table's DataBodyRange starting at the next available row.

5
Restore Excel settings and close the source

Add an error-handling section that closes the text file, restores Application.ScreenUpdating = True, and reports the error before the macro ends.

Match the source structure: Update the delimiter, section markers, worksheet name, table mapping, and column count to match the actual TXT file. Marker-based parsing is more reliable than fixed row offsets when sections vary in length.

Process TXT files and organize imported data in WPS Spreadsheets

WPS Office can open and organize spreadsheet data in a familiar worksheet interface. Import the TXT content, split fields with the correct delimiter, and place the cleaned records into a structured table; macro availability and VBA compatibility can vary by WPS edition, so use the built-in import tools when a macro is not supported.

  1. 1. Open WPS Spreadsheets: Launch WPS Office, choose Spreadsheets, and open the destination workbook or create a new spreadsheet.
  2. 2. Import the text file: Choose Data > From Text/CSV or the available text import command, select the TXT file, and set the delimiter and text encoding in the import preview.
  3. 3. Review the parsed fields: Check that records appear in separate columns, remove blank rows if needed, and verify that section labels and variable-length records remain identifiable.
  4. 4. Format the destination range as a table: Select the imported range, choose Insert > Table, confirm that the range has headers when applicable, and apply the required column formatting.
Import delimited TXT content and organize variable-length records in a structured worksheet.Use a familiar spreadsheet interface to review blank lines, delimiters, and section markers.Open and edit Microsoft Excel .xlsx and .xlsm files with broad format compatibility.Lightweight tools make it easier to inspect imported data before saving or sharing the workbook.
microsoft office alternative - wps office

Frequently Asked Questions

How do I import a TXT file into an existing Excel table with VBA?

Use Application.GetOpenFilename to select the file, read each line with Line Input, skip blank lines, split each record with the correct delimiter, and write the fields to the next worksheet row or a new ListObject row.

How can VBA handle TXT sections with different numbers of lines?

Use section labels, headers, or other markers to identify the current section. Do not calculate the next section from a fixed row number because the number of lines can change between files.

Why does the imported data appear in one column?

The Split delimiter probably does not match the source file. Inspect the TXT file and replace the comma in Split(line, ",") with vbTab, a semicolon, pipe, or the actual separator.

How should blank lines be handled during the import?

Trim each line and test whether it is empty before processing it. Skip empty lines so they do not create blank rows in the RawData worksheet or table.

What should the macro do if a record has fewer fields than expected?

Check the number of elements returned by Split before writing the row. Log or skip incomplete records, or fill missing table columns with empty values according to the required data structure.