|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub kong()
- strFile = ThisWorkbook.Path & "\新建文本文档.txt" ''txt 路径
- Set fso = CreateObject("Scripting.FileSystemObject")
- ar = Split(readfile(strFile), vbCrLf)
- '''写入 Open ..Output最后会保留空行,这种不会
- Set f = fso.OpenTextFile(strFile, 2, True)
- For i = 1 To UBound(ar) - 1
- If Len(ar(i)) Then '''非空写入
- If ar(i) = "54654654" Then ar(i) = "aaaa"
- f.Writeline ar(i)
- End If
- Next
- If Len(ar(UBound(ar))) Then f.Write ar(UBound(ar)) ''最后一行写入
- f.Close
- Set fso = Nothing
- Set f = Nothing
- End Sub
- '读取文件
- Function readfile(ByVal filename As String, Optional filetype As String = "utf-8")
- Dim ReadStream As Object
- Set ReadStream = CreateObject("ADODB.Stream")
- Dim FileContent As String
- With ReadStream
- .Type = 2 'adTypeText
- .Charset = filetype
- .Open
- .LoadFromFile filename
- FileContent = .ReadText
- .Close
- End With
- Set ReadStream = Nothing
- readfile = FileContent
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|