我有这段代码,它循环遍历文本文件列表,在 Excel 中打开它们,并清除其中的引号。但是,由于某种我不明白的神秘原因,如果没有引号,它就不会保存。我尝试了不同的方法来清除引号,但仍然不起作用。
我需要代码来打开其他文本文件并删除每个文件中的引号。
dim pathfile as string
dim pathway as workbook
dim localcode as string
dim wb as workbook
dim wayfile as string
dim i as integer
application.screenupdating = false
application.displayalerts = false
wayfile = "pathfile"
set wb = workbooks.open(wayfile)
range ("A1").Select
i = 1
do while not isempty(range("a" & i))
math = activecell
pathfile = "pathfile"
pathfile = replace(pathfile, "math", math)
set pathway = workbooks.open(pathfile)
range("A1").select
do while activecell.value <> ""
activecell.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=FalseFormulaVersion:=xlReplaceFormula2
activecell.offset(1,0).select
loop
pathway,saveas fileformat:=xlTextWindows
pathway.close
pathfile = "pathfile2"
pathfile = replace(pathfile2, "math", math)
set pathway = workbooks.open(pathfile)
range("A1").select
do while activecell.value <> ""
activecell.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=FalseFormulaVersion:=xlReplaceFormula2
activecell.offset(1,0).select
loop
pathway,saveas fileformat:=xlTextWindows
pathway.close
set wb = workbooks.open(camin)
range("A1").Select
activecell.offset(1,0).select
i = i + 1
loop
application.screenupdating = true
application.displayalerts = true
wb.close
msgbox "ok"
你确实不需要 Excel 来做这样的事情。直接打开文本文件,将数据加载到数组中,替换文本并将其写回文本文件。这就是你正在尝试的吗?
Option Explicit
Sub Sample()
Dim MyData As String, strData() As String
Dim FlName As String
Dim i As Long
'~~> Change this to the relevant text file
FlName = "C:\Users\routs\Desktop\test.txt"
'~~> Open text file in 1 go and read it into an array
Open FlName For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)
'~~> Loop through an array and replace double quotes
For i = LBound(strData) To UBound(strData)
strData(i) = Replace(strData(i), Chr(34), "")
Next i
'~~> Write back to text file.
Open FlName For Output As #1
Print #1, Join(strData, vbCrLf)
Close #1
End Sub