View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JLatham JLatham is offline
external usenet poster
 
Posts: 3,365
Default How to read text from a non-delimited text file?

That's what's known as fixed-length field file. Excel can actually import
them directly to a spreadsheet using the data import | Text type feature.

But you can work with them in VBA easily enough. I just open the text file
and start reading it line by line and parse each line as it's read in using
MID$() function.

Goes something like this (shows hard coded filename, but you can get it
other ways)
Dim textFile As String
Dim rawData As String
Dim filenum As Integer

textFile = "C:\My Documents\MyData\newData.txt"
filenum = Freefile()
Open textFile For Input As #filenum
Do While Not EOF(Filenum)
Line Input #filenum, rawData
myVar = Mid(rawData, 7,11)
....code continues
Loop
Close #filenum

The Mid function grabs characters starting at the first # position for the
second # number of characters, so start at 7th characters for 11 characters =
columns 7 through 17, inclusive.

"Phil" wrote:

I would like to use a macro to read in text from a text file that is
not delimited and store the data in variables. For example, from line
5 of the text file I want to read from column 7 to 17 and store that
value in a variable. After reading in values from various places in
the file, I will do some calculations using these numbers, then create
a new Excel file and write the calculated numbers into the new file.

Is there a way to do this in VBA?