回复 2楼 apull
要处理的文件,放在桌面上。
下面代码生成的结果会一行空行,后面会多三行空格。不知怎么去掉,求指教。
我的代码如下:
程序代码:
Private Sub Form_Load()
Me.Hide
Dim strArr() As String
Dim strArr2() As String
Dim StrLine As String
Dim buffer As String
Open "C:\Users\Admin\Desktop\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, buffer
StrLine = StrLine + buffer + vbCrLf
Loop
StrLine = Left(StrLine, Len(StrLine) - 1) '读取文件的全部内容,并去除最后一个回车符
Close #1
strArr = Split(StrLine, vbCrLf) '分割成一维数组
lenArr = UBound(strArr) '一维数组的数量
ReDim strArr2(lenArr, 1) '定义一个二维数组
For i = 0 To lenArr '分割成二维数组
t = Split(strArr(i), ":")
strArr2(i, 0) = t(0) '记录数据的文字部分
strArr2(i, 1) = t(1) '记录数据的数字部分
Next
For i = 0 To lenArr - 1
For j = i To lenArr
If (CLng(strArr2(i, 1)) > CLng(strArr2(j, 1))) Then
t1 = strArr2(i, 0)
t2 = strArr2(i, 1)
strArr2(i, 0) = strArr2(j, 0)
strArr2(i, 1) = strArr2(j, 1)
strArr2(j, 0) = t1
strArr2(j, 1) = t2
End If
Next
Next
StrLine = ""
For i = 0 To lenArr '组合字符串
StrLine = StrLine & strArr2(i, 0) & ":" & strArr2(i, 1) & vbCrLf
Next
StrLine = Left(StrLine, Len(StrLine) - 1) '去除最后面一个回车符
Open "C:\Users\Admin\Desktop\2.txt" For Output As #1
Print #1, StrLine
Close #1
End Sub