早上好
我有一个名为“txt”的txt文件,有4行。 而我的问题是,我想删除最后3行中的最后2个单词。删除txt文件中的最后一个字
这是我在TXT文本:
"vol"
"vui one high" one high
"vui one front " two high
"vuil high front " three high
,并删除的话被引号后的2。
我有一个代码,但在这一刻它只允许更换一个字 这里是我的代码开始:
Sub CommandButton1_Click()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
' Edit as needed
sFileName = "C:\Users\bquinty\Desktop\txt.txt"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "word to remplace", "word wanted")
'Save txt file as (if possible)
iFileNum = FreeFile
sFileName = "C:\Users\bquinty\Desktop\txt.txt"
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
感谢所有帮助/建议,你可以给我吗。
您可以使用['split'](http://www.exceltrick.com/formulas_macros/vba-split-function/ )函数将'sTemp'分成单行(分隔符应该是vbcrlf或vbcr取决于你的txt文件)。然后再次“拆分”将最后2行分成单词(分隔符为空)。之后,您只需删除最后2个条目并将字符串重新放在一起。 –
文件中是否只有4行或可以更改?你是说你想要文件只包含引号之间的数据,或者如果还有更多的话你仍然只想删除最后两个? – CLR