• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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