完全披露 - 这里不是程序员...... 我在这个网站上找到的代码取得了一些成功,可以很好地改变文本文件中的各个行。但我只需要在同一文本文件中更改文本字符串的最后一次出现。下面的代码会在文件中附加正确的更改,但这不是我需要它做的。我需要它彻底改变" G00 Z.5"到" G00 Z7.0"。以下是我到目前为止......
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
我需要最后一次出现" G00 Z.5"要更改为" G00 Z7.0",但不附加正在创建的新文件。你能帮忙吗?
答案 0 :(得分:1)
尝试一下:
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
答案 1 :(得分:0)
你可以尝试这样:
WIX