7,789
社区成员
发帖
与我相关
我的任务
分享'对提取出来的数据进行排序操作
Sub TEST()
Dim a As String, b() As String
Dim filename As String
Dim tmp As String '其实用 a 就可以,加一个变量仅仅为了提高易读性
Dim i As Integer, j As Integer, n As Integer
filename = ThisDrawing.Path + "\" + Split(ThisDrawing.Name, ".")(0) + ".txt"
Open filename For Binary As #1
'打开txt文件,文件名自己改
a = Input(LOF(1), #1)
'把整个txt文件的内容读入字符串变量a中
b = Split(a, vbCrLf)
'这时候b()数组中即包含了txt文件中的每行数据
'这样就可以对b()数组进行各种排序操作了
'具体如何排序就不知道了
'......
For i = 0 To Ubound(b) - 1
If Val(Mid(b(i), 2) > i + 1 Then
tmp = b(i)
For j = i + 1 To Ubound(b)
If Val(Mid(b(j), 2) = i + 1 Then
b(i) = b(j)
b(j) = tmp
Exit For
End If
Next j
End If
Next i
a = Join(b, vbCrLf)
'b()数组排序后重新组合为一个字符串a
Put #1, 1, a
'把处理后的数据重新写入文件
Close #1
End Sub
Dim Strline As String
Dim i As Integer
Dim n As Integer
Dim m As String
Dim j As Integer
Dim a() As String
Dim b() As String
FileName = ThisDrawing.Path + "\" + Split(ThisDrawing.Name, ".")(0) + ".txt"
'*****读取数据文件********************************
Open FileName For Input As #1
Do '开始读行循环
Line Input #intFilenum, Strline '读行
a = Split(Strline, ";") '将行数据分割到数组中
n = UBound(a) '获得数据的最后一个的下标
m = a(n - 1) '获得你要排号的数字
b(m) = Strline '将数据读入到数组中
i = i + 1
Loop While Not EOF(1) '读文件循环到文件尾
Close #1
'*****输出数据文件********************************
Open FileName & "2.txt" For Output As #2
For j = 1 To i
Print #2, b(j) '按排号输出
Next j
Close #2
'对提取出来的数据进行排序操作
Sub TEST()
Dim a As String, b() As String
Dim filename As String
filename = ThisDrawing.Path + "\" + Split(ThisDrawing.Name, ".")(0) + ".txt"
Open filename For Binary As #1
'打开txt文件,文件名自己改
a = Input(LOF(1), #1)
'把整个txt文件的内容读入字符串变量a中
b = Split(a, vbCrLf)
'这时候b()数组中即包含了txt文件中的每行数据
'这样就可以对b()数组进行各种排序操作了
'具体如何排序就不知道了
'......
a = Join(b, vbCrLf)
'b()数组排序后重新组合为一个字符串a
Put #1, 1, a
'把处理后的数据重新写入文件
Close #1
End Sub
Private Sub Command1_Click()
Dim fileName As String
Dim lngFile As Long
Dim strFile As String
Dim arr() As String, arrSort() As String
Dim Idx As Long
Dim i As Long
'读出文件内容到strFile
fileName = "d:\test1.txt"
lngFile = FreeFile
Open fileName For Binary As lngFile
strFile = Space(LOF(lngFile))
Get #lngFile, , strFile
Close
'按行分组
arr = Split(strFile, vbCrLf)
'如果序号是从1开始的话如下处理
ReDim arrSort(UBound(arr))
arrSort(0) = arr(0)
For i = 1 To UBound(arr)
If Len(Trim(arr(i))) > 0 Then
Idx = Val(Mid(arr(i), 2)) '得到当前行的序号
If Idx > 0 Then arrSort(Idx) = arr(i) '赋值到排序数组序号对应下标的元素中
End If
Next
'写回新文件
fileName = "d:\test2.txt"
lngFile = FreeFile
Open fileName For Output As lngFile
Print #lngFile, Join(arrSort, vbCrLf)
Close
'运行结果
Shell "C:\Windows\NOTEPAD " & fileName, vbNormalNoFocus
End Sub