• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

Such a thing as End Of Variable when reading from var? Like EOF when reading from file?

I'd like to be able to read from a variable, line by line, w/o having to first count the number of lines in the variable.  Loop until EOF works great when reading from a file via line input and it'd be great if I could use something similar to that when reading from a variable (line by line).

I'm asking because I need to "flip" the contents of a .txt file and make the bottom (last entry) the top (first) and vice-versa. The only way I know how to do this is to read the file, line by line and dump it into a new file (or back into its own). Thanks.
0
JohnDoeSr
Asked:
JohnDoeSr
  • 4
1 Solution
 
vinnyd79Commented:
You can split it into an array using something like this:

Dim MyVar as string
Dim arrLines() as string

MyVar = "your variable data ........"

arrLines = Split(MyVar,vbCrLf)

' loop through array
Dim x as Long
For x = 0 to Ubound(arrLines)
   MsgBox arrLines(x)
Next x
0
 
vinnyd79Commented:
You could rebuild the variable backwards then write it back to the file:

Dim x As Long
MyVar = ""
For x = UBound(arrLines) To LBound(arrLines) Step -1
   If MyVar = "" Then
     MyVar = arrLines(x)
   Else
     MyVar = MyVar & vbCrLf & arrLines(x)
   End If
Next x

Open "Myfile.txt" for output as #1
Print #1, MyVar
Close #1
0
 
vinnyd79Commented:
Maybe put it into a function:


Private Sub Command1_Click()
FlipFile "C:\MyFile.txt"
End Sub


Private Sub FlipFile(sFileName As String)
Dim ff As Integer, fData As String
Dim arrLn() As String, x As Long
ff = FreeFile
Open sFileName For Input As #ff
fData = Input$(LOF(ff), ff)
Close #ff

arrLines = Split(fData, vbCrLf)

ff = FreeFile
Open sFileName For Output As #ff
For x = UBound(arrLines) To LBound(arrLines) Step -1
    If Trim$(arrLines(x)) <> "" Then
        Print #ff, arrLines(x)
    End If
Next x
Close #ff
End Sub
0
 
vinnyd79Commented:
uhh, I meant a sub not a function.
0
 
JohnDoeSrAuthor Commented:
Wow, I was ready to accept your first comment but ate lunch first then came back and you added a bunch more useful info. Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now