7,763
社区成员
发帖
与我相关
我的任务
分享
Private Function GetText4TXT(ByVal TxtName As String, ByVal TxtField As Long) As String()
Dim tmpStrBuff As String, tmpStrBuffArr1() As String, tmpStrBuffArr2() As String, tmpOutBuffArr() As String
Dim I As Long, J As Long
Open TxtName For Binary As #1 '先一次性读入文件
tmpStrBuff = String(LOF(1), Chr(0))
Get 1, , tmpStrBuff
Close #1
TxtField = TxtField - 1
tmpStrBuffArr1 = Split(tmpStrBuff, vbCrLf) '按行分割
J = 0
ReDim tmpOutBuffArr(J) '初始化输出数组,0维不使用.
For I = 0 To UBound(tmpStrBuffArr1) '从每行开始循环,把所需要的列复制到输出数组内
tmpStrBuffArr2 = Split(tmpStrBuffArr1(I), vbTab) '目标行是用什么分开每列的这里就用什么
If UBound(tmpStrBuffArr2) > 2 And UBound(tmpStrBuffArr2) >= TxtField Then
J = J + 1 '加一维,把结果复制过去.
ReDim Preserve tmpOutBuffArr(J)
tmpOutBuffArr(J) = Trim(tmpStrBuffArr2(TxtField))
Debug.Print tmpOutBuffArr(J)
End If
Next I
GetText4TXT = tmpOutBuffArr
End Function
文件:
1 2 3 4
2 3 4 5
12 6 7 6
Type Record
Field1 as String*6
Field2 as String*10
Field2 as String*8
Field2 as String*12
End Type
Dim r As Record, i As Integer
Open "myfile" For Random As #1 Len = Len(r)
For i = 1 To Int(LOF(1) / Len(r))
Get #1, i, r
Debug.Print Val(r.Field2)
Next i
Close #1
Dim tmp As String, fields() As String
Open "myfile" For Input As #1
Do Until EOF(1)
Line Input #1, tmp
Do While Instr(tmp, Space(2))
tmp = Replace(tmp, Space(2), Space(1))
Loop
fileds = Split(tmp, Space(1))
Debug.Print fields(1)
Loop
Close #1
Private Sub Command1_Click()
Dim a As Variant
Dim b As Variant
a = Split(Text1, vbCrLf) '分割text1内的数据成行
For i = 0 To UBound(a) '循环每一行
If Len(a(i)) > 3 Then '这个判断用来避免把空行提取算进来 若提取到空行则提取不到数组b(1)了
c = (a(i))
For j = 0 To Len(a(i)) '这个循环把多余的空格全部变成一个空格
c = Replace(c, " ", " ")
Next j
b = Split(c, " ") '以一个空格划分后把分段放到数组b中
s = s & b(1) & vbCrLf '字符=字符连接b的第2项目连接换行
End If
Next i
Text1 = s '重新输出到text
End Sub
Private Sub Form_Load()
a = "1 2 3 4" & vbCrLf
a = a & "1 3 5 8" & vbCrLf
a = a & "123 325 587 18" & vbCrLf
a = a & "81 355 55 a8" & vbCrLf
a = a & "1c 2c3 585 c8" & vbCrLf
Text1 = a
End Sub