Home>

Hello
I'm using VBA

Reads data line by line from a text file, and when a particular character is found, it extracts two lines of data from it and then advances to the next line
At this time, I want to show an error screen when the next line that finds a specific character is empty, but I don't know what to do
Below is the code for the loop part

Do Until EOF (txtnum)
   'Reading the second line
   Line Input #txtnum, line1
   If InStr (line1, "Tokyo")>0 Then
       tokyoFlg = "1"
   Else
   If tokyoFlg = "1" Then
      'The second line is empty
        If line1 = "" Then
   MsgBox "Processing is terminated because invalid data is included"
        End
        End If
Read 3rd line
        Line Input #txtnum, line2
      'Get date of second row
        data = getdata (line1, data)
'The third line is empty
If line2 = "" Then
  ∙ MsgBox "The processing is terminated because invalid data is included"
       End
End If
      'Get the 3rd line location
        citydata = getcitidata (line2, citydata)
 Print #filenum, data, citydata
      'Initialize
      data = ""
      citydata = ""
      tokyoFlg = ""
End If
g = g + 1
End If
Loop

Images of text files include
Tokyo
2018/08/29 Hale
Ota-ku
Tokyo
2018/08/30
Setagaya-ku cloudy

I want to extract the necessary part from two lines from the line called Tokyo in this way and output it. If there is currently two lines of data from Tokyo, it will end as normal without error,
For example,

Tokyo
2018/08/29 cloudy
Ota-ku
Tokyo
2018/08/29

I want to give an error and end processing when there is no data for two lines in this way
Without data
Read line 3
Line Input #txtnum, line2
Error occurs at the part
Of course, you can't read the next line, so you can see an error, but the first line from Tokyo (date, weather line) exists from Tokyo, and I don't know how to handle the error if there is no next

Thank you.

Additional
Thanks for your answers
BA is the simplest solution

  • Answer # 1

    First of all, please do not use "End".
    This kills the program,
    Exit Do, Exit For, Exit Function, Exit Sub, etc. are safer.

    It is easier to see the indentation as shown below.

    And the processing is the first data set in Tokyo, so
    If you count the number of data that way,
    Processing is clear and easy.

    Because it is difficult to maintain multiple writing processes
    I feel that it is easier to understand if only one place is used.

    And close the text file properly at the end of the process.

    Do Until EOF (txtnum)
            G = G + 1
            'Read one line
            Dim LineVal As String
            Line Input #txtnum, LineVal
            If InStr (LineVal, "Tokyo")>0 Then
                Dim LineNo As Long
                LineNo = 1
            Else
                LineNo = LineNo + 1
            End If
            Select Case LineNo
            Case 1 'Tokyo
                'Count the number of data sets
                Dim DataNo As Long
                DataNo = DataNo + 1
            Case 2 'Date
                'The second line is empty
                If LineVal = "" Then
                    MsgBox "End processing because invalid data is included"
                    Exit Do
                End If
                'Get date of second row
                Dim DateVal As String
                DateVal = GetData (LineVal, Data)
            Case 3 'Ku
                '3rd line is empty
                If LineVal = "" Then
                    MsgBox "End processing because invalid data is included"
                    Exit Do
                End If
                'Get the 3rd line location
                citydata = getcitidata (LineVal, citydata)
                'writing
                Print #FileNum, Data, citydata
                'Initialize
                Data = ""
                citydata = ""
            End Select
        Loop
        'Close processing (required for both txtnum and FileNum)

  • Answer # 2

    I think that it is more efficient to read and process everything after calling the following function without error.

    * pathSRC: Full Path of the file

    Usage example:

    txtTEST = ReadTxt ("C: \ Temp \ Test.txt")
    For Each L In Split (txtTEST, vbCrLf)
    ...
    Next

    Function ReadTxt (pathSRC) As String
        Set objFS = CreateObject ("Scripting.FileSystemObject")
        ReadTxt = objFS.OpenTextFile (pathSRC, 1) .ReadAll
        Set objFS = Nothing
    End Function

  • Answer # 3

    I wonder if EOF has read it anyway.
    Since the current read result is determined by whether it is an empty character or not, it should be an error if the read data is empty when it is not EOF or EOF.
    That's why I think that making Line Input a function is a quick response.

    'Read processing
    Function GetLine (txtnum As Integer) As String
        If EOF (txtnum) Then
            GetLine = ""
        Else
            Line Input #txtnum, GetLine
        End If
    End Function


    The Line Input is like this.

    Line Input #txtnum, line1
         ↓
       line1 = GetLine (txtnum)