Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 11949|回复: 14

[已解决]怎么样将 txt文件里所有换行符替换成空

[复制链接]
发表于 2014-3-17 11:16 | 显示全部楼层 |阅读模式
求助啊代码要怎么写
最佳答案
2014-3-17 12:36
  1. Sub test()
  2.     Dim strFile$
  3.     Dim strPath$
  4.     Dim strTemp$
  5.     strPath = ThisWorkbook.Path & Application.PathSeparator
  6.     strFile = "test.txt"
  7.     Open strPath & strFile For Input As #1
  8.     strTemp = StrConv(InputB(LOF(1), #1), vbUnicode)
  9.     Close #1
  10.     Open strPath & strFile & "-new.txt" For Output As #1
  11.     strTemp = Replace(strTemp, vbCrLf, " ")
  12.     Print #1, strTemp
  13.     Close #1
  14. End Sub
复制代码
写入的,不过是写入到了新文件。
发表于 2014-3-17 11:49 | 显示全部楼层
回复

使用道具 举报

发表于 2014-3-17 12:30 | 显示全部楼层
本帖最后由 hwc2ycy 于 2014-3-17 12:32 编辑
  1. Sub test()
  2.     Dim strFile$
  3.     Dim strPath$
  4.     Dim strTemp$
  5.     strPath = ThisWorkbook.Path & Application.PathSeparator
  6.     strFile = "test.txt"
  7.     Open strPath & strFile For Input As #1
  8.     strTemp = StrConv(InputB(LOF(1), #1), vbUnicode)
  9.     Close #1
  10.     MsgBox strTemp
  11.     MsgBox Replace(strTemp, vbCrLf, " ")
  12. End Sub
复制代码
回复

使用道具 举报

发表于 2014-3-17 12:32 | 显示全部楼层
Desktop.rar (15.13 KB, 下载次数: 8)
回复

使用道具 举报

发表于 2014-3-17 12:32 | 显示全部楼层
代码只演示了把TXT里的换行符换成了空格,通过msgbox输出。
如果要在写回文件,只需要稍微改变即可。
回复

使用道具 举报

发表于 2014-3-17 12:36 | 显示全部楼层    本楼为最佳答案   
  1. Sub test()
  2.     Dim strFile$
  3.     Dim strPath$
  4.     Dim strTemp$
  5.     strPath = ThisWorkbook.Path & Application.PathSeparator
  6.     strFile = "test.txt"
  7.     Open strPath & strFile For Input As #1
  8.     strTemp = StrConv(InputB(LOF(1), #1), vbUnicode)
  9.     Close #1
  10.     Open strPath & strFile & "-new.txt" For Output As #1
  11.     strTemp = Replace(strTemp, vbCrLf, " ")
  12.     Print #1, strTemp
  13.     Close #1
  14. End Sub
复制代码
写入的,不过是写入到了新文件。
回复

使用道具 举报

 楼主| 发表于 2014-3-17 14:19 | 显示全部楼层
hwc2ycy 发表于 2014-3-17 12:36
写入的,不过是写入到了新文件。

strpath这是做什么用的?
回复

使用道具 举报

 楼主| 发表于 2014-3-17 14:20 | 显示全部楼层
hwc2ycy 发表于 2014-3-17 12:36
写入的,不过是写入到了新文件。

运行时在十一行报错
回复

使用道具 举报

发表于 2014-3-17 14:29 | 显示全部楼层
linatt 发表于 2014-3-17 14:19
strpath这是做什么用的?

文件的位置。


回复

使用道具 举报

发表于 2014-3-17 14:38 | 显示全部楼层
linatt 发表于 2014-3-17 14:20
运行时在十一行报错

哪一行出错,是第二个代码么。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-18 23:17 , Processed in 0.453079 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表