Exporting and Import Text With Excel
Exporting and Import Text With Excel
This page describes how to import text files to Excel and how to export Excel data out to
text files.
Introduction
This pages describes VBA procedures that you can use to export data from an Excel
worksheet to a text file and to import data from a text file directly into an existing range
of a worksheet.
These procedures are for delimited file, where each field is of (optionally) different
lengths and fields are separated by a separator character such as a comma.
See Exporting To A Fixed Width File for code to export Excel data to a fixed width text
file. See Importing A Fixed Width File for code to import a fixed width text file into Excel.
Sep The character that is to separate the elements on each row of the exported file. Typically, this is vbTab, a space,
a comma, semicolor, or pipe ( | ). Any character may be used.
SelectionOnly If True, only the currently selected cells are exported. If False, the entire used range of the worksheet is
exported.
AppendData If True and FName exists, data is written to the end of the text file, preserving the existing contents. If False, the
existing contents of FName are destroyed and only the newly exported data will appear in the output file.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExportToTextFile
' This exports a sheet or range to a text file, using a
' user-defined separator character.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportToTextFile(FName As String, _
Sep As String, SelectionOnly As Boolean, _
AppendData As Boolean)
Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ExportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub DoTheExport()
ExportToTextFile FName:="C:\Test.txt", Sep:=";", _
SelectionOnly:=False, AppendData:=True
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DoTheExport
' This prompts the user for the FileName and the separtor
' character and then calls the ExportToTextFile procedure.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub DoTheExport()
Dim FileName As Variant
Dim Sep As String
FileName =
Application.GetSaveAsFilename(InitialFileName:=vbNullString,
FileFilter:="Text Files (*.txt),*.txt")
If FileName = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Sep = Application.InputBox("Enter a separator character.",
Type:=2)
If Sep = vbNullString Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Debug.Print "FileName: " & FileName, "Separator: " & Sep
ExportToTextFile FName:=CStr(FileName), Sep:=CStr(Sep), _
SelectionOnly:=False, AppendData:=True
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END DoTheExport
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
This code will prompt the user for a text file name in which to save the file and for the
separator character. If the user cancels either of these dialogs, the procedure is
terminated and no export operation is carried out.
If your text file contains greater the 64K records, or you need to split the imported text
across multiple worksheets, see Importing Big Text Files Into Excel.
The ImportToTextFIle procedure is shown below. FName is the name of the file to import
and Sep is the character that separates column data.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ImportTextFile
' This imports a text file into Excel.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ImportTextFile(FName As String, Sep As String)
Application.ScreenUpdating = False
'On Error GoTo EndMacro:
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.row
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ImportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub
Since ImportTextFile takes input parameters, it should be called from other VBA code:
Sub DoTheImport()
ImportTextFile FName:="C:\Test.txt", Sep:="|"
End Sub
In this code, both the file name and the separator are hard coded into the code. If you
want to prompt the user for a file name and separator character, use code like the
following:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DoTheImport
' This prompts the user for a FileName as separator character
' and then calls ImportTextFile.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub DoTheImport()
Dim FileName As Variant
Dim Sep As String
FileName = Application.GetOpenFilename(FileFilter:="Text File
(*.txt),*.txt")
If FileName = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Sep = Application.InputBox("Enter a separator character.",
Type:=2)
If Sep = vbNullString Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Debug.Print "FileName: " & FileName, "Separator: " & Sep
ImportTextFile FName:=CStr(FileName), Sep:=CStr(Sep)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END DoTheImport
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''