Excel vba能不能处理文本文档
答案:4 悬赏:80 手机版
解决时间 2021-11-18 20:37
- 提问者网友:蓝琪梦莎
- 2021-11-18 05:28
Excel vba能不能处理文本文档
最佳答案
- 五星知识达人网友:撞了怀
- 2021-11-18 06:57
给你一个例子自己学习一下,这是一个读写操作,基本过程就是
把已给路径的txt文件逐行读取到一个缓存里,但人为添加了回车换行,修改后,在会传到这个文件中
Public Sub TXTRWPractice()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
' Edit as needed
sFileName = ActiveWorkbook.Path & "\123.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, "DIM A", "1.75")
sTemp = Replace(sTemp, "DIM B", "2.00")
sTemp = Replace(sTemp, "DIM C", "3.00")
sTemp = Replace(sTemp, "DIM D", "4.00")
'Save txt file as (if possible)
'写回传文件
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
'Close Userform
'Unload UserForm1
End Sub
把已给路径的txt文件逐行读取到一个缓存里,但人为添加了回车换行,修改后,在会传到这个文件中
Public Sub TXTRWPractice()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
' Edit as needed
sFileName = ActiveWorkbook.Path & "\123.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, "DIM A", "1.75")
sTemp = Replace(sTemp, "DIM B", "2.00")
sTemp = Replace(sTemp, "DIM C", "3.00")
sTemp = Replace(sTemp, "DIM D", "4.00")
'Save txt file as (if possible)
'写回传文件
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
'Close Userform
'Unload UserForm1
End Sub
全部回答
- 1楼网友:持酒劝斜阳
- 2021-11-18 09:50
可以,我就会!
- 2楼网友:往事隔山水
- 2021-11-18 08:18
肯定可以的
- 3楼网友:第四晚心情
- 2021-11-18 07:06
这个是可以的
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯