VBA & Macro Problems

Import Text Files into Excel Worksheets with VBA

Kushani NimanthikaKushani Nimanthika Dec 2, 2026 868 views

Question details

Import multiple text files with VBA, using the existing first worksheet for the first file and new worksheets for later files.

Import Text Files Starting with the First Excel Worksheet
Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook has an empty first worksheet before a VBA text-file import begins.
Observed behavior
The import routine creates additional worksheets instead of using the existing first sheet.
Before you start

Open the target workbook in desktop Excel and confirm its first worksheet is empty, since the import starts at cell A1. Save the workbook as an .xlsm file and enable macros if prompted.

Solution 1Recommended

Import the first file into the existing worksheet

Use this VBA routine to select multiple text files and add worksheets only after the first import.

The routine uses ThisWorkbook, so place the macro in the workbook that should receive the imported data. The example expects tab-delimited files.

1
Open the VBA editor

In Excel, press Alt+F11, select the target workbook in Project Explorer, and choose Insert > Module.

2
Add the import macro

Paste this code into the module: Sub ImportTextFiles() Dim wb As Workbook Dim ws As Worksheet Dim picker As FileDialog Dim filePath As Variant Dim fileNumber As Long Set wb = ThisWorkbook Set picker = Application.FileDialog(msoFileDialogFilePicker) With picker .Title = "Select text files" .AllowMultiSelect = True .Filters.Clear .Filters.Add "Text files", "*.txt" If .Show <> -1 Then Exit Sub End With fileNumber = 0 For Each filePath In picker.SelectedItems fileNumber = fileNumber + 1 If fileNumber = 1 Then Set ws = wb.Worksheets(1) Else Set ws = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count)) End If With ws.QueryTables.Add(Connection:="TEXT;" & CStr(filePath), Destination:=ws.Range("A1")) .TextFileParseType = xlDelimited .TextFileTabDelimiter = True .Refresh BackgroundQuery:=False End With Next filePath End Sub

3
Run the import

Return to Excel, open Developer > Macros, select ImportTextFiles, and click Run. Select the text files in the file picker and click Open.

4
Check the imported sheets

Inspect the first worksheet and the newly added sheets. If an unrelated blank sheet remains, verify that it is not needed, then right-click its sheet tab and choose Delete.

Import the first file into the existing worksheet
Match the file format: For comma-delimited files, replace .TextFileTabDelimiter = True with .TextFileCommaDelimiter = True. The first selected file is imported into the first worksheet; later files receive new sheets.
Free Microsoft Office alternative

Work with spreadsheet files in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Office files. It can help you move between spreadsheet applications, though it does not fix Excel's VBA routine and macro support may vary.

Open and work with spreadsheet data in WPS SpreadsheetsCompatible with Microsoft Excel formats such as XLSX and XLSFamiliar spreadsheet interface for an easier transitionFree, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does my macro leave the first worksheet blank?

The routine may add a worksheet before importing every file. Assign the first import to wb.Worksheets(1), and call wb.Worksheets.Add only for later files.

How do I import comma-delimited text instead?

In the QueryTables settings, use .TextFileCommaDelimiter = True instead of .TextFileTabDelimiter = True. Check the imported columns after running the macro.

Can I delete an unused blank sheet after importing?

Yes. First confirm that the sheet is not needed and that the workbook will retain at least one visible worksheet. Then right-click the sheet tab and choose Delete.

Which workbook receives the imported files?

The example uses ThisWorkbook, meaning the workbook that contains the macro. Put the code in the target workbook or change the wb assignment to an explicit reference to the workbook you want to use.