VB,怎样写代码在目录下的TXT中指定一行末尾加上指定的文字?

来源:百度知道 编辑:UC知道 时间:2024/09/23 18:19:48
一个按钮,2个label,目录下有个123.txt
txt的格式大概是这样:
88888888,ABC,8,888888,888888,888888
888888,AB,88,888888,888888,888888,888888,888888
…………
怎么写这个按钮的代码能让他找出txt中,第一个逗号前的内容与Label1.Caption的内容相匹配的一行,并在这一行后面加上"," & Mid(Label2.Caption, 3, 2) & Mid(Label2.Caption, 6, 2) & Mid(Label2.Caption, 9, 2)
请高人赐教,附上说明
写写

'记得先备份你的123.txt哦!

Private Sub Command1_Click()
Dim ReDat As String '存读入的文字
Dim MidDat As String '第一个逗号前的内容
Dim SavDat() As String '处理之后的内容
Dim i As Variant '用作数组的下标,j同
Dim j As Variant
Open App.Path & "\123.txt" For Input As #1 '打开文件
Do While Not EOF(1) '循环读取到文件尾
Line Input #1, ReDat '读入一行
MidDat = Left(ReDat, InStr(1, ReDat, ",", vbTextCompare) - 1) '得到第一个逗号前的内容
If MidDat = Label1 Then '如果与Label1.Caption的内容相匹配的一行,并在这一行后面加上:
ReDat = ReDat & "," & Mid(Label2.Caption, 3, 2) & Mid(Label2.Caption, 6, 2) & Mid(Label2.Caption, 9, 2)
End If
ReDim Preserve SavDat(i) As String
SavDat(i) = ReDat '记录处理之后的内容
i = i + 1
Loop
Close
Open App.Path & "\123.txt" For Output As #2 '打开文件
For j = 0 To i - 1