Excel 替换文本文件中最后出现的文本字符串
完全公开-这里不是一个程序员。。。 我在这个网站上找到了一些代码,可以很好地更改文本文件中的单行。但我只需要更改同一文本文件中最后出现的文本字符串。下面的代码将正确的更改附加到文件中,但这不是我需要它做的。我需要它完全改变了“G00Z.5”到“G00Z7.0”。下面是我到目前为止所拥有的Excel 替换文本文件中最后出现的文本字符串,excel,vba,Excel,Vba,完全公开-这里不是一个程序员。。。 我在这个网站上找到了一些代码,可以很好地更改文本文件中的单行。但我只需要更改同一文本文件中最后出现的文本字符串。下面的代码将正确的更改附加到文件中,但这不是我需要它做的。我需要它完全改变了“G00Z.5”到“G00Z7.0”。下面是我到目前为止所拥有的 Sub TestMacro1() Dim sBuf As String Dim sTemp As String Dim iFileNum As Integer Dim sFile
Sub TestMacro1()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
Dim NewFileName As String
Dim Pos As Long
Dim Str As String
Dim rplStr As String
'Edit as needed
sFileName = "Z:\Code-Unedited\TestText.txt"
NewFileName = "Z:\Code-Edited\TestText.OUT"
iFileNum = Freefile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Str = sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "T01 01", "T2001")
rplStr = StrReverse(Replace(StrReverse(Str), StrReverse("G00 Z.5"), StrReverse("G00 Z7.0"), , 1))
'Save txt file as
iFileNum = Freefile
Open NewFileName For Output As iFileNum
Print #iFileNum, sTemp
Print #iFileNum, rplStr
Close iFileNum
End Sub
这是之前的文本文件
这是我的文本文件
N03 T01 01 M06
G00 Z.5
G00 Z.5
N03 T2001 M06
G00 Z.5
G00 Z.5
G00 Z7.0
这是…之后的输出
这是我的文本文件
N03 T01 01 M06
G00 Z.5
G00 Z.5
N03 T2001 M06
G00 Z.5
G00 Z.5
G00 Z7.0
我需要将最后一次出现的“G00 Z.5”更改为“G00 Z7.0”,但不要像当前那样附加创建的新文件。你能帮忙吗?你能这样试试吗:
Str = Replace(Str, "T01 01", "T2001")
Str = StrReverse(Replace(StrReverse(Str), StrReverse("G00 Z.5"), StrReverse("G00 Z7.0"), , 1))
iFileNum = FreeFile
Open NewFileName For Output As iFileNum
Print #iFileNum, Str
尝试一下:
Sub tgr()
Dim oFSO As Object
Dim sFilePath As String
Dim sOldText As String
Dim sNewText As String
Dim sTemp As String
Dim aAllFindReplace() As String
Dim sLastFind As String, sLastReplace As String
Dim aLines() As String
Dim i As Long, j As Long
Dim LastFound As Boolean
sFilePath = Application.GetOpenFilename("Text Files,*.txt", , "Select Text File", , False)
If sFilePath = "False" Then Exit Sub 'Pressed cancel
Set oFSO = CreateObject("Scripting.FileSystemObject")
sOldText = oFSO.OpenTextFile(sFilePath).ReadAll
aLines = Split(sOldText, vbCrLf)
LastFound = False
'Change as necessary
ReDim aAllFindReplace(1 To 12, 1 To 2)
For i = LBound(aAllFindReplace, 1) To UBound(aAllFindReplace, 1)
aAllFindReplace(i, 1) = "T" & Format(i, "00") & " " & Format(i, "00") 'T01 01; T02 02; etc
aAllFindReplace(i, 2) = "T2" & Format(i, "000") 'T2001; T2002; etc
Next i
sLastFind = "G00 Z.5"
sLastReplace = "G00 Z7.0"
'Loop backwards to ensure last occurrence is found first
For i = UBound(aLines) To LBound(aLines) Step -1
sTemp = aLines(i)
If Len(sTemp) > 0 Then
'if you have any general replacements that need to happen, replace those here
'replacements made here will occur for every instance
For j = LBound(aAllFindReplace, 1) To UBound(aAllFindReplace, 1)
sTemp = Replace(sTemp, aAllFindReplace(j, 1), aAllFindReplace(j, 2), Compare:=vbTextCompare)
Next j
If InStr(1, sTemp, sLastFind, vbTextCompare) > 0 And LastFound = False Then
'If you have any replacements that should ONLY happen on the last occurrence, replace that here
'Replacements made here will only happen on the last occurrence
sTemp = Replace(sTemp, sLastFind, sLastReplace, Compare:=vbTextCompare)
LastFound = True
End If
sNewText = sTemp & vbCrLf & sNewText
End If
Next i
sNewText = Left(sNewText, Len(sNewText) - Len(vbCrLf)) 'Remove ending newline
'Output the file with the replacements in same location as a .OUT file
With oFSO.CreateTextFile(Replace(sFilePath, ".txt", ".OUT", Compare:=vbTextCompare))
.WriteLine sNewText
.Close
End With
Set oFSO = Nothing
Erase aLines
End Sub
将所有出现的“G00 Z.5”更改为“G00 Z7.0”。我只需要它来改变最后一个事件。它在文件的后面附加了一行。@GSamler-你是在评论我的编辑还是之前的代码?我有点想不通你为什么需要
Str
,sTemp
和rplStr
?谢谢。我是在评论代码,这是这个网站的新成员。我使用sTemp来更改代码行,这些代码行上有T01 01、T02 02、T03 03等等。我试图使用rplStr将最后一次出现的“G00 Z.5”更改为G00 Z7.0”。@GSamler是的,我能为您提供什么帮助?我在“一般替换”下添加了这个:因为我需要这些…sTemp=Replace(sTemp,“T01 01”,“T2001”)sTemp=Replace(sTemp,“T02 02”,“T2002”)sTemp=Replace(sTemp,“T03 03”,“T2003”)sTemp=Replace(sTemp,“T04 04”,“T2004”)sTemp=Replace(sTemp,“T05 05”,“T2005”),但它只是打开了一个没有文件名的窗口&它没有做任何事情。我也没有更改“最后一行”“,这是我描述的错误,我试图更改G00 Z的最后一次出现。5您的代码如何知道正在编辑的文件的路径名?@GSamler请参阅更新的代码。至于如何知道路径名,代码会提示您选择一个文本文件,并将该文件路径和文件名存储在变量sFilePath