read windows notepad file content into excel?

G

Guest

Greetings
I have many notepad files that i pick through manually and copy/paste info
out to excel. The notepad file content is logically structured ('bananas =
yellow' in file1, 'bananas = green' in file2 etc. sort of thing). Is it
possible for an excel macro to retrieve external file content like this?
Many Thanks
 
M

moon

Two methods:

'Method 1.
Public Sub ImportTextFile1()
Dim txtFile, txtTemp, FileContents As String
Dim fileNum As Integer
txtFile = "C:\SAMPLE.txt"
FileContents = vbNullString
txtTemp = vbNullString
fileNum = FreeFile()
Open txtFile For Input As #fileNum
Do While Seek(fileNum) <= LOF(fileNum)
Line Input #fileNum, txtTemp
FileContents = FileContents & txtTemp & vbCrLf
Loop
Close #fileNum
MsgBox FileContents
End Sub

'Method 2.
Public Sub ImportTextFile2()
Dim fso, f
Dim FileContents As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\SAMPLE.txt")
FileContents = f.ReadAll
f.Close
MsgBox FileContents
End Sub
 
N

NickHK

David,
Excel can open text files with the Text Import wizard.
Just open your file from File>Open and follow the steps. I imagine you need
to set the "Delimited" option, then specify the Delimiter "Other" to "=".
If you record a macro whilst do this, you will get the code.

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top