Loading Text File into Array

E

ExcelMonkey

I am trying to load a text file into an array using the following code below.
I have two questions:

1) Why can I not put variable declarations after the Fname$ and Raw$? I get
errors when I try to use String and Variant. Doe the $ denote a variable
type?

2) The code is failing on Txt$ = Split(Raw$, vbCrLf). Its failing because
the text string has multiple line spacing in it (See excerpt of text below).
It there a way to get around this? I am assuming I need a variable parameter
instead of vbCrLf.

Sub GetTextFile()
Dim Fname$
Dim ff As Integer
Dim Raw$

ff = FreeFile
Fname$ = "C:\Documents and Settings\kaczanor\Desktop\Sample.txt"

Open Fname$ For Binary As #ff
Raw$ = String$(LOF(ff), 32)
Get #ff, 1, Raw$
Close #ff
Txt$ = Split(Raw$, vbCrLf)

End Sub
'**********************************************
'Text String Excerpt:

Summary
Parties
Grounds
Decision on costs
Operative part


Keywords


Freedom of movement for persons - Freedom of establishment - Company formed
in accordance with the law of a Member State in which it has its registered
office but in which it conducts no business - Establishment of a branch in
another Member State - Registration refused - Not permissible - Member States
free to adopt measures to combat fraud

(EC Treaty, Arts 52 and 58)


Summary

'*********************************************************


Thanks

EM
 
R

Rick Rothstein

1) Yes, the $ sign tagged after a variable name is old-style for "As
String". So, these two lines of code do the same thing...

Dim Var$

Dim Var As String

And, personally, I hate using it (as well as the other old-style type
declaration characters).

2) The Split function only returns results to a dynamically declared array;
since you didn't Dim your Txt$ variable, it becomes a simple Variant
variable, not an array. Try adding this declaration and your code should
work fine...

Dim Txt$()
 

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