Home > Software engineering >  Vb to read CSV file, the output in the text box
Vb to read CSV file, the output in the text box

Time:10-21

For example: the CSV file content is: Date, $Time, PV1, p2v, PV3, PV11, PV12, PV13, PV4, SP, T1, T2, T3, T4, T5, T6, T7 has, T8, T10, T9, T11, T12, YOUYA, VZ1, V2, V3 and V4 and V5
,97,105,16.8 03/23/12 17:40:26, 102102102102103102, 17.5, 22.1, 26.3, 21.1, 21.4, 22.3, 19,22.3, 23,25.4, 22.8, 49668,0,0,0,10.091, 843
,17.1 03/23/12 17:45:26, 110110110110109110105112, 17.8, 23,28.2, 22.1, 22.5, 23,19,22.3, 23.2, 25.8, 22.8, 47544,0,0,0,10.091, 790
,16.8 03/23/12 17:50:26, 118118118118117118113120, 18,24,30.5, 23.2, 23.7, 24,19.5, 22.5, 23.5, 26.8, 23451, 41,0,0,0,10.091 807
,16.8 03/23/12 17:55:26, 125126126126126125121128, 18,24.9, 32.9, 24.4, 24.7, 25.1, 19.7, 22.5, 23.7, 27.5, 23454 07,0,0,0,10.091, 813
,17.1 03/23/12 18:00:26, 133133133133134133128135, 18.3, 26.1, 35.2, 26.1, 26.1, 26.1, 20,22.3, 24,28,23.2, 46939,0,0,0,10.091, 820
Want to output the same no comma phase in the text box, the contents of the column of
$Date $Time PV1 p2v PV3 PV11 PV12 PV13 PV4 SP T1 T2, T3, T4 T5 T6 T7 has T8 T9 T10 T11 T12 YOUYA VZ1 V2 V3 V4 V5
03/23/12 17:40:26 102 102 102 102 103 102 97 105 16.8 17.5 22.1 26.3 21.1 21.4 22.3 and 22.3 and 25.4 22.8 49668 0 0 0, 843
10.09103/23/12 17:45:26 110 110 110 110 109 110 105 112 17.1 17.8 28.2 22.1 22.5 23 19 10.091 22.3 23.2 25.8 22.8 47544 0 0 0, 790
03/23/12 17:50:26 118 118 118 118 117 118 113 120 18 24 24 19.5 22.5 23.5 30.5 23.2 23.7 16.8 26.8 23 45141 0 0 0, 807
10.09103/23/12 17:55:26 18, 125, 126, 126 126 126 125 121 128 16.8 24.9 32.9 24.4 24.7 25.1 19.7 22.5 23.7 27.5 23 45407 0 0 0, 813
10.09103/23/12 18:00:26 133 133 133 133 134 133 128 135 17.1 18.3 26.1 35.2 26.1 26.1 26.1 22.3 24 to 28, 23.2 46939 0 0 0, 820
10.091I became program
Private Sub Command6_Click ()
Dim TextLine
Dim TextString
Dim SplitStr As the Variant
Dim SplitStr2 As the Variant
Dim As Long, I j As Long
Dim s As String
Dim m, n As Integer
Dim (a) As String
Open "DATAFILE. CSV" For Input As # 1 'Open the file,
The Do While Not EOF (1) 'circular end of the file to,
Line Input # 1, the TextLine 'read in a row and give it a variable,
Debug. Print TextLine 'in the Debug window display data,
If TextString="" Then
TextString=TextLine
The Else
TextString=TextString & amp; ";" & TextLine
End the If
Loop
Close # 1 'Close the file,

SplitStr=Split (TextString, ";" )
M=UBound (SplitStr)
For I=0 To m
SplitStr2=Split (SplitStr (I), ", ")
N=UBound (SplitStr2)
ReDim a (0 To m, 0 To n)
For j=0 To n
A (I, j)=SplitStr2 (j)
Text1. Text=Text1. Text + a (I, j) & amp; "
"Next j
Text1. Text=Text1. Text & amp; VbCrLf
Next I
End Sub
If the CSV file content too much, running special slow and even crash

CodePudding user response:

Open "DATAFILE. CSV" For Input As # 1
Text1=Replace (Input (LOF (1), 1), ", ", "")
Close # 1

CodePudding user response:

The moderator you too much, if so, the CSV file for the following (a small, for example, the actual have 28 column, many lines),
Date, $Time, PV1 p2v, PV3
03/23/12, 17:40:26, 102102102
03/23/12, 17:45:26, 110110110
03/23/12, 17:50:26, 118118118
03/23/12, 17:55:26, 125126126
03/23/12, 18:00:26, 133133133
I did five check box: Date respectively $Time PV1 p2v PV3, which one is selected, only the extracted from CSV column, and the output from a text box?
So I made up which CSV file contents to a two dimensional array? How should?
Thank you very much

CodePudding user response:

At the same time also want to send this to EXCEL in the text box content? Given guidance, I just contact with VB soon

CodePudding user response:

refer to the second floor heianzhiwang response:
the moderator you too much, if like this: the CSV file for the following (a small, for example, the actual have 28 column, many lines),
Date, $Time, PV1 p2v, PV3
03/23/12, 17:40:26, 102102102
03/23/12, 17:45:26, 110110110
03/23/12, 17:50:26, 118118118
03/23/12, 17:55:26, 125126126
03/23/12, 18:00:26, 133133133
I did five check box: Date respectively $Time PV1 p2v PV3, which one is selected, only the extracted from CSV column, and the output from a text box?
So I made up which CSV file contents to a two dimensional array? How should?
Thank you very much

Using the Split function cutting
For example:
Dim arr () As String
S="03/23/12, 17:40:26, 102102102,"
Arr=Split (s, ", ")
The arr (0) is "03/23/12
"Arr (1) is "17:40:26
".
Combined with your CheckBox is ok,

CodePudding user response:

My program seems to be to do so, but if the CSV file data is too much, and then assigned to the array output text box, particularly slow
 Private Sub Command6_Click () 
Dim TextLine
Dim TextString
Dim SplitStr As the Variant
Dim SplitStr2 As the Variant
Dim As Long, I j As Long
Dim s As String
Dim m, n As Integer
Dim (a) As String
Open "DATAFILE. CSV" For Input As # 1 'Open the file,
The Do While Not EOF (1) 'circular end of the file to,
Line Input # 1, the TextLine 'read in a row and give it a variable,

If TextString="" Then
TextString=TextLine
The Else
TextString=TextString & amp; ";" & TextLine
End the If
Loop
Close # 1 'Close the file,

SplitStr=Split (TextString, ";" )
M=UBound (SplitStr)
For I=0 To m
SplitStr2=Split (SplitStr (I), ", ")
N=UBound (SplitStr2)
ReDim a (0 To m, 0 To n)
For j=0 To n
A (I, j)=SplitStr2 (j)
Text1. Text=Text1. Text + a (I, j) & amp; "
"Next j
Text1. Text=Text1. Text & amp; VbCrLf
Next I
End Sub

Is not an array can also
  • Related