ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Find Data (https://www.excelbanter.com/excel-programming/388827-find-data.html)

CWillis

Find Data
 
This shouldn't be hard, I just can't figure it out...

I have a text file with text at the top. Maybe some numbers too. Later in
the text file, there are two columns of data. The goal is to get excel to
get these two rows of data so I can plot, analyze, etc.

Any ideas to get started?
Thanks for the help.
-Chris

joel

Find Data
 
Ther are a number of different methos for reading text files into excel.
Sometimes using the Data Menu - Import External data works great. Sometimes
you need to read the text file with Notepad and extract the data you want
before using the Import external data. Other times I have to write my own
input filters to read the data. One discussion that you have to make is the
number of times you are going to have to read the data. If you are going to
need to read the data only once, then I recommend editing the data with
notepd and then reading the data into excel using Input External Data method.

Below is a templet for reading text files and applying your own filter.


Sub ReadFile()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const Part_Path = "D:\Visual Basic\Shapes"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Set fs = CreateObject("Scripting.FileSystemObject")

First = True

'read all text files in directory
Do While (1)

'get filenames
If First = True Then
PathName = Part_Path + "\" + "*.txt"
Filename = Dir(PathName)
First = False
Else
Filename = Dir
End If

If Filename = "" Then Exit Do

'open file
PathName = Part_Path + "\" + Filename
Set f = fs.GetFile(PathName)
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)

'check if worksheet exists
'get sheet name
sheetname = Left(Filename, InStr(Filename, ".") - 1)
FoundWS = False
For Each ws In Worksheets

If ws.Name = sheetname Then
FoundWS = True
Sheets(ws.Name).Activate
Exit For
End If
Next ws

If FoundWS = False Then
Worksheets.Add after:=ActiveSheet
ActiveSheet.Name = sheetname
End If

'read file
Do While ts.atendofstream = False
Line = ts.ReadLine

Loop

ts.Close
Loop


End Sub

"CWillis" wrote:

This shouldn't be hard, I just can't figure it out...

I have a text file with text at the top. Maybe some numbers too. Later in
the text file, there are two columns of data. The goal is to get excel to
get these two rows of data so I can plot, analyze, etc.

Any ideas to get started?
Thanks for the help.
-Chris



All times are GMT +1. The time now is 12:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com