Read A Text File With VBA in Excel, and Write The Text To A Spreadsheet
Read A Text File With VBA in Excel, and Write The Text To A Spreadsheet
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.
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.
Press Alt+F11 to bring up the Visual Basic editor. To create a new module, right-click your
workbook's name in the editor's project window and choose Insert, Module.
''======================================================
'' Program: ParseText
'' Desc: Reads a text file into a variable then
'' writes it into a row, n chars at a time
'' Called by: user
'' Call:
'' Arguments:
'' Comments: Written quickly. No error-checking.
'' Changes----------------------------------------------
'' Date Programmer Change
'' 6/14/06 Charley Kyd Written
''======================================================
Sub ParseText()
Dim sText As String, sFile As String
''======================================================
'' Program: GetText
'' Desc: Read a text file into a string and then
'' return the string
'' Called by: ParseText
'' Call: GetText(sFile)
'' Arguments: sFile--The full path to the text file
'' Comments:
'' Changes----------------------------------------------
'' Date Programmer Change
'' 6/14/06 Charley Kyd Written
''======================================================
Function GetText(sFile As String) As String
Dim nSourceFile As Integer, sText As String
GetText = sText
End Function
''======================================================
'' Program: WriteToSheet
'' Desc: Writes a text string to one row of a
'' worksheet, n characters per column
'' Called by: ParseText
'' Call: WriteToSheet sText
'' Arguments: sText--String with text from text file
'' Comments: This routine will throw an error if the
'' text is too long to fit within 256 columns
'' Changes----------------------------------------------
'' Date Programmer Change
'' 6/14/06 Charley Kyd Written
''======================================================
Sub WriteToSheet(sText As String)
Dim sTgtSheet As String, nTgtRow As Integer
Dim nColCount As Integer, sChunk As String
Dim nIncrement As Integer, rngRef As Range
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.