【发布时间】:2016-02-16 13:42:52
【问题描述】:
Filename = Dir(Filepath & "\" & "*.csv")
While Filename <> ""
SourceFile = Filepath & "\" & Filename
TargetFile = SavePath & "\" & Replace(Filename, ".csv", ".txt")
OpenAsUnicode = False
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
'Detect Unicode Files
Dim Stream: Set Stream = objFSO.OpenTextFile(SourceFile, 1, False)
intChar1 = Asc(Stream.Read(1))
intChar2 = Asc(Stream.Read(1))
Stream.Close
If intChar1 = 255 And intChar2 = 254 Then
OpenAsUnicode = True
End If
'Get script content
Set Stream = objFSO.OpenTextFile(SourceFile, 1, 0, OpenAsUnicode)
arrData = Stream.ReadAll()
Stream.Close
'Create output file
Dim objOut: Set objOut = objFSO.CreateTextFile(TargetFile)
objOut.Write Replace(Replace(arrData,",", "#|#"), Chr(34), "") '-- This line is working fine but it is replacing all the commas inside the text qualifier as well..
objOut.Close
Filename = Dir
Wend
在上面的代码中,objOut.Write Replace(Replace(arrData,",", "#|#"), Chr(34), "") 行将所有逗号替换为#|#,包括里面的逗号string.so 我只想替换不在双引号中的逗号。
包含字符串的文件 "A","B,C",D
我需要的结果是 A#|#B,C#|#D
提前感谢您的帮助。
【问题讨论】:
-
这是谷歌上的第一首热门歌曲。请先搜索。 stackoverflow.com/questions/6780765/…
-
谢谢,但是您提供的链接将更改单个字符串的分隔符如何将其应用于循环中的多个文件.. 如果您可以更改我的代码,我不是 VBA 专家很大的帮助...
-
改变你替换为用#|#替换“,”而不是仅仅替换,
-
Mathew - 抱歉,我没有正确回答您的问题
-
你到底想做什么?用井号重写文件的目的是什么?
标签: vba