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
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,
I want to give an error and end processing when there is no data for two lines in this way
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
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
txtTEST = ReadTxt ("C: \ Temp \ Test.txt")
For Each L In Split (txtTEST, vbCrLf)
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)
- cannot read file with vba adodbstream
- python - read a text file and write to another text file
- JAVA read text file content example code
- C # paging read GB text file example
- python - how to read a text file and distribute it
- php and c implementation read file by line example code
- C # to quickly read txt text data into excel
- C # processing text file TXT examples
- java read txt file code snippet
- vba error handling "" range "method failed:" _global "object"
- Nodejs read file example
- js read and write json file example code
- C # method to read csv format file
- VC read configuration file example
- C # method to read txt file line by line
- C # implementation code to read file MD5 value
- js read csv file and display it using json
- Java recursive read file example
- C ++ file read and write code sharing
- Java read TXT file content method
- python - how to know the arguments of a vba function in a protected excel file
- vba - i want to transfer to a cell that meets two conditions
- vba error occurs after the second time
- vba sheet 1 a7, c7, sheet 2 a2 "2020" 03 "01" numerical value is added and displayed in the cell as "20
- the process is interrupted when i click the button on the web page with vba
- vba - i want to copy the last line and paste it to the last line +1
- vba - using the entirerow property
- vba - i want to divide the sheet by the same data
- about excel vba loop
- vba - how to apply change events to multiple cells at the same time