Read A Text File With VBA in Excel
Read A Text File With VBA in Excel
VBA can read a text file and then write it to your spreadsheet. Here's an example of a VBA program
that shows how to do it.
"I need to write a text file into one row of my Excel spreadsheet, cell by cell, 20 characters at a
time. It's urgent. Can you help?" -- Kumar
Kumar,
It's sort of a strange request. But it sounds like fun. And it demonstrates how we can read text files
into Excel using VBA.
By the way, the following code illustrates the various points I discussed in Corporate VBA Standards
For Excel Users Who Program.
To begin, create a new workbook with two sheets. Name one sheet Control and the other Target.
http://www.exceluser.com/blogdata/images/post_900_108/vba_textcols.gif
Assign the range names shown in column A to cells in column B. To do so, select the range A4:B9.
Choose Insert, Name, Create. In the Create Names dialog, ensure that only Left Column is checked.
Then choose OK.
Enter the path and name for your text file. (I used File Explorer to navigate to its directory and then
copied the path text from the Address bar.) Enter the other information shown.
Option Explicit
''======================================================
'' Call:
'' Arguments:
'' Changes----------------------------------------------
''======================================================
Sub ParseText()
With ThisWorkbook
sFile = .Names("SourcePath").RefersToRange
End With
''Get the full text string from the text file
sText = GetText(sFile)
sText = Excel.WorksheetFunction.Clean(sText)
WriteToSheet sText
End Sub
''======================================================
'' Comments:
'' Changes----------------------------------------------
''======================================================
Close
''Get the number of the next free text file
nSourceFile = FreeFile
sText = Input$(LOF(1), 1)
Close
GetText = sText
End Function
''======================================================
'' Changes----------------------------------------------
''======================================================
With ThisWorkbook
sTgtSheet = .Names("TargetSheet").RefersToRange
nTgtRow = .Names("TargetRow").RefersToRange
nIncrement = .Names("Increment").RefersToRange
End With
rngRef.EntireRow.ClearContents
nColCount = 0
Do
nColCount = nColCount + 1
nIncrement, nIncrement)
Finally, set up a button in your Control sheet to run the macro easily. To do so, first activate your
Control sheet. Right-click any toolbar. Click on Forms if its not already checked.
Click on the Button icon in the Forms toolbar. Doing so turns your pointer into a cross. Use the
cross to draw the outline of a button on your worksheet.
When you release your left mouse button, Excel draws the button and launches the Assign Macro
dialog. Choose the ParseText macro, then choose OK. Select the text "Button 1" in the button and
then type any text you want, like "Parse Text". Then click on any cell to deselect the button.
Now, when you click on the button, Excel should run your macro and write your text to your Target
worksheet.