求助下面代码VBA如何实现对文本文件删除、替换及保存!
OpenfilenameForInputAs#filenumberDoWhileNotEOF(filenumber)LineInput#filenumber,textli...
Open filename For Input As #filenumber
Do While Not EOF(filenumber)
Line Input #filenumber, textline
ss= replace(textline,"aaa",ACB,1,1)
此处想删除原textline的内容,将SS(替换后的内容)写到文本文件中(在原位置:比如textline读取第20行数据,删除第20行数据,同时将ss内容写入第20行)
保存文本文件或者另存为文本文件
loop
求高人解救,不甚感激!谢谢!!!! 展开
Do While Not EOF(filenumber)
Line Input #filenumber, textline
ss= replace(textline,"aaa",ACB,1,1)
此处想删除原textline的内容,将SS(替换后的内容)写到文本文件中(在原位置:比如textline读取第20行数据,删除第20行数据,同时将ss内容写入第20行)
保存文本文件或者另存为文本文件
loop
求高人解救,不甚感激!谢谢!!!! 展开
1个回答
展开全部
在全文各处做替换,请参看:
Private Sub Loadtxt()
Dim strData(1 To 10000) As String
TotalLines = 0
strFileName = Application.GetOpenFilename("CSV Files (*.txt), *.txt", , "Open TXT File", , False)
On Error GoTo LEND
Open strFileName For Input As #1
While Not EOF(1)
Line Input #1, textline
TotalLines = TotalLines + 1
strData(TotalLines) = Replace(textline, "aaa", "ACB", 1, 1)
Wend
Close #1
Open strFileName For Output As #1
For i = 1 To TotalLines
Print #1, strData(i)
Next i
Close #1
LEND:
End Sub
Private Sub Loadtxt()
Dim strData(1 To 10000) As String
TotalLines = 0
strFileName = Application.GetOpenFilename("CSV Files (*.txt), *.txt", , "Open TXT File", , False)
On Error GoTo LEND
Open strFileName For Input As #1
While Not EOF(1)
Line Input #1, textline
TotalLines = TotalLines + 1
strData(TotalLines) = Replace(textline, "aaa", "ACB", 1, 1)
Wend
Close #1
Open strFileName For Output As #1
For i = 1 To TotalLines
Print #1, strData(i)
Next i
Close #1
LEND:
End Sub
追问
谢谢FLIPZD,用你上面这种方式只能是替换一次文本就要重新将文件写入,我可能有很多次的替换,这样效率太低,我已经找到解决方法了,谢谢!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询