Thanks
Ben!
Sub GetTextInfo()
Dim MyLine As String
Dim MyVars() As String
Dim VarCount As Integer
Dim nFile As Integer
Dim Cntr As Integer
Dim SingleDig As String
VarCount = 0
nFile = FreeFile
Open ("C:\clc\acadmenu\DETAILS\DETAILS.txt") For Input As #nFile
Do While Not EOF(nFile)
Input #nFile, MyLine
For Cntr = 0 To Len(MyLine) - 1
SingleDig = Right(MyLine, Cntr)
If SingleDig = "|" Then
ReDim Preserve MyVars(0 To VarCount, 0 To 1)
MyVars(VarCount, 0) = Left(MyLine, Len(MyLine) - (Cntr + 1))
MyVars(VarCount, 1) = Right(MyLine, Cntr)
End If
Next Cntr
Loop
Close nFile
End Sub
Okay, I made a couple of changes to the above code (mainly adding the
nFile[/i] variable and using
FreeFile[/i] then closing the file at the end) and, I admit, this is where I'm having my main problems at the moment.
First of all, in the snippet you posted, at what point does the variable
VarCount ever change to anything other than
0?
And I'm going to try to take this code line by line and explain it so that any of you reading this can see if I understand what's going on.
Please, please, please correct me when I'm wrong:
Sub GetTextInfo()
Dim MyLine As String
Dim MyVars() As String
Dim VarCount As Integer
Dim nFile As Integer
Dim Cntr As Integer
Dim SingleDig As String
Simple enough, I'm naming this routine
GetTextInfo and I'm declaring the variables and their type.
VarCount = 0
nFile = FreeFile
Open ("C:\clc\acadmenu\DETAILS\DETAILS.txt") For Input As #nFile
I'm setting the value of
VarCount to
0 and I'm setting the value of
nFile to the next available free file number. Then I open my text file for input and tell the system that the file identified as
nFile is in fact the text file I specified.
Do While Not EOF(nFile)
Input #nFile, MyLine
Here I'm telling the system to keep doing what I tell it to until it gets to the end of my text file then I'm reading my text file for input. I'm setting the variable
MyLine to equal the first line of text in the text file.
For Cntr = 0 To Len(MyLine) - 1
Get the length (or number of characters) of the variable and set the variable
Cntr to equal that number. Set a factor of
-1 to be the increment that the variable
Cntr changes each time it steps through the
For loop.
SingleDig = Right(MyLine, Cntr)
If SingleDig = "|" Then
ReDim Preserve MyVars(0 To VarCount, 0 To 1)
MyVars(VarCount, 0) = Left(MyLine, Len(MyLine) - (Cntr + 1))
MyVars(VarCount, 1) = Right(MyLine, Cntr)
End If
Next Cntr
Set the variable
SingleDig to equal the last character in the variable
MyLine, our line of text from our text file. If the character encountered is the pipe ("
|") then this is where I begin to get confused.
According the the VBA help file concerning the
ReDim statement:
The subscripts argument uses the following syntax:
[lower To] upper [,[lower To] upper] . . .
When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.
Also
The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).
You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array. However, you can't declare an array of one data type and later use ReDim to change the array to another data type, unless the array is contained in a Variant. If the array is contained in a Variant, the type of the elements can be changed using an As type clause, unless you’re using the Preserve keyword, in which case, no changes of data type are permitted.
If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array. The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.
Okay, so in english, what does the next line of our code say? I have no idea.
But, putting that aside I'll assume, for the moment, that it does something spectacular. The next line, I believe, sets the first element in the variable
MyVars to equal anything to the left of the pipe symbol. If that is, in fact what it's doing then the next line should be setting the second element in the
MyVars variable to anything to the right of the pipe symbol. I could be wrong though.
Being stuck on what's supposed to be happening with that
ReDim statement has my brain twisted in knots.
That being said, it appears that the
For loop continues on to the proceeding character, working it's way backwards, in the line of text if the character it's reading right now
isn't the pipe symbol.
Loop
Close nFile
End Sub
Not the end of our file yet? Loop back and do this all again until it is. Once finished, close the text file we opened and end our subroutine.
How's that so far?