我想用vb编个程序,读取文本文件中每行里第一个逗号左面的数据,这个应该怎么弄呀,谢谢给位大神赐教!
2个回答
展开全部
'这个代码有要求:每个数据不能大于8个字符(tab键默认的所占的字符长度)
Private Sub Form_Click() '请点击窗体
Me.Cls
Dim xRow, yLine, i, j As Long, Temp As String, Arr(1 To 99) As String
xRow = Val(InputBox("输入行数:"))
yLine = Val(InputBox("输入列数:"))
Me.AutoRedraw = True: j = 1
Open "C:\\Documents and Settings\\Administrator\\桌面\\1.txt" For Input As #1 '文件路径
For i = 1 To xRow
Input #1, Temp
Next i
Close #1
For i = 1 To Len(Temp)
If Mid(Temp, i, 1) = Chr(9) Then
Arr(j) = Left(Temp, i - 1): Temp = Replace(Temp, Arr(j) & Chr(9), "", , 1)
j = j + 1: i = 0
Else
If i = 9 Then
Arr(j) = Trim(Left(Temp, i - 1)): Temp = Replace(Temp, Arr(j), "", , 1)
j = j + 1: i = 0
End If
End If
If Len(Temp) = 0 Then Exit For
Next i
Print Arr(yLine)
End Sub
又或者用下面的代码,没有上面的要求了:
Private Sub Form_Click() '请点击窗体
Me.Cls
Dim xRow, yLine, i, j As Long, Temp As String, Arr
xRow = Val(InputBox("输入行数:"))
yLine = Val(InputBox("输入列数:"))
Me.AutoRedraw = True: j = 1
Open "C:\\Documents and Settings\\Administrator\\桌面\\1.txt" For Input As #1 '文件路径
For i = 1 To xRow
Input #1, Temp
Next i
Close #1
Arr = Split(Temp, Chr(9))
Print Arr(yLine - 1)
End Sub
Private Sub Form_Click() '请点击窗体
Me.Cls
Dim xRow, yLine, i, j As Long, Temp As String, Arr(1 To 99) As String
xRow = Val(InputBox("输入行数:"))
yLine = Val(InputBox("输入列数:"))
Me.AutoRedraw = True: j = 1
Open "C:\\Documents and Settings\\Administrator\\桌面\\1.txt" For Input As #1 '文件路径
For i = 1 To xRow
Input #1, Temp
Next i
Close #1
For i = 1 To Len(Temp)
If Mid(Temp, i, 1) = Chr(9) Then
Arr(j) = Left(Temp, i - 1): Temp = Replace(Temp, Arr(j) & Chr(9), "", , 1)
j = j + 1: i = 0
Else
If i = 9 Then
Arr(j) = Trim(Left(Temp, i - 1)): Temp = Replace(Temp, Arr(j), "", , 1)
j = j + 1: i = 0
End If
End If
If Len(Temp) = 0 Then Exit For
Next i
Print Arr(yLine)
End Sub
又或者用下面的代码,没有上面的要求了:
Private Sub Form_Click() '请点击窗体
Me.Cls
Dim xRow, yLine, i, j As Long, Temp As String, Arr
xRow = Val(InputBox("输入行数:"))
yLine = Val(InputBox("输入列数:"))
Me.AutoRedraw = True: j = 1
Open "C:\\Documents and Settings\\Administrator\\桌面\\1.txt" For Input As #1 '文件路径
For i = 1 To xRow
Input #1, Temp
Next i
Close #1
Arr = Split(Temp, Chr(9))
Print Arr(yLine - 1)
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询