0

New here and have been searching for a method to read from a text file which makes use of commas as a delimiter. I want to assign the text to a variable and use it within the program. For instance, a line from the text file would be

45.325, Variable Description

And I need to assign 45.325 to a variable name in the code. I found a method which uses oFS.Readline, but it reads the whole line whereas I just need it to read up to (not including) the comma.

Thanks for your response Ben

pagid
  • 13,559
  • 11
  • 78
  • 104
Bjamham
  • 1
  • 1

1 Answers1

0

Several different options are available, depending on what exactly you're trying to do.

If you only want the first value before the comma, you can use the old OPEN FOR INPUT approach. The "Input #ff, FirstValue" literally loads the first value encountered (up until a comma is reached) into that variable:

Public Sub SampleRead(TextFile As String)
  Dim ff As Integer, i As Long
  Dim FirstValue As Double
  Dim VariableName As String
  ff = FreeFile
  Open TextFile For Input As #ff
    Do Until EOF(ff)
      i = i + 1
      Input #ff, FirstValue
      Input #ff, VariableName
      Debug.Print "Variable Name: " & VariableName & " has a value of: " & FirstValue & " on line #" & i
    Loop
  Close #ff
End Sub    

Alternately, you could use Split() to parse the lines of the text file you're reading in with .Readline (you can use "Line Input" to do the same thing) in order to break them out into the specific variables.

C. White
  • 802
  • 1
  • 7
  • 19