Home > Software engineering >  Read the text. The TXT characters to array what went wrong
Read the text. The TXT characters to array what went wrong

Time:10-07

I this code below, is to import a TXT file, the inside address book information group, contents such as the

Zhang SAN 13900000323
Li si 13821654466
Li 139216565465
Wang wei's 13546871231

Normally no problem, but if free middle or come to the last line, will go wrong,

At the same time, sometimes I between name and mobile phone is used in the XLS direct copy, the middle is the TAB, rather than space, also make mistakes, I add on the error resume next will be stuck there,
Excuse me,
1 how to prevent the error free guild?

2 how can compatible if not space, but the TAB also can achieve the same function?

Three small problems, I select files, choose a good no problem, cancel will go wrong, this should be easy to solve, but I won't, consult,









The code is as follows:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *



 Dim (a) As String, b () As String 'definition of dynamic array 
Dim (d) As String
Dim j As Integer

"' array

Private Sub Command1_Click () 'import contacts to import address book
"' On the Error Resume Next
CDl. Filter="text file | *. TXT | all file | *. *", "

CDl. ShowOpen
TXT file path. The Text=CDl. FileName
' ' ' 'On the Error Resume Next
The address book
2. The Caption="total" & amp; J & amp; "Home"
Start. Enabled=True
End Sub "'" '"' the above import address book import contacts at or above the import directory

Private Sub directories ()

"' On the Error Resume Next
I=0

Open TXT file path For the Input As # 1 'opens the file

The Do While Not EOF (1) 'read data cycle
ReDim Preserve a (I) 'the number of the specified array keyword is reserved Preserve array data
ReDim Preserve b (I)
Line Input # 1, c $' read data, assigned to string c
On the Error Resume Next
C=LTrim (c) 'remove Spaces in front of the

On the Error Resume Next
D=Split (c, "") 'the c into an array, assign d array, with a space for delimiters
A (I)=d (0) 'd array data is assigned to a (), b ()
J=1 'deal with the issue of multiple space between the two data
The Do While d (j)=""
J=j + 1
Loop
B (I)=d (j)

I=I + 1 'add the number of array b a 1
Loop
The Close 'Close the file
Text2=""
Text1=""
For j=0 To I - 1 'print it out, check over, isn't it right

Text1=Text1 & amp; A (j) & amp; VbCrLf
Text2=Text2 & amp; B (j) & amp; VbCrLf
Next

End Sub

CodePudding user response:

On the Error Resume Next
Is the best way to cover a program error. Don't use everywhere

CodePudding user response:

Under the reprocessing under
C=Trim (Replace (c, VbTab, ""))
If c Like "* *" Then
.

CodePudding user response:

 c=Trim (c) 
The Do While InStr (c, Space (2))
C=Replace (c, Space (2), Space (1))
Loop
C=Replace (c, Space (1), vbTab)
D=Split (c, vbTab)

CodePudding user response:

Thank you thank you thank you!
  • Related