Excel 2007 VBA 宏逐行读取文本文件如何停止以逗号 (,) 分隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5706944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:23:21  来源:igfitidea点击:

Excel 2007 VBA Macro reading a text file line by line how do I stop delimiting on a comma (,)

vbafile-ioexcel-vbaexcel

提问by codingguy3000

I have a simple Excel 2007 Macro that is reading a text file line by line and displaying the output.

我有一个简单的 Excel 2007 宏,它正在逐行读取文本文件并显示输出。

This macro is breaking on commas. I want it to simply read the entire line breaking on a carrage return.

这个宏打破了逗号。我希望它在回车时简单地读取整个换行符。

What am I doing wrong?

我究竟做错了什么?

Sub Directory()
  Dim strFileName As String
  Dim strDirectory As String
  Dim intFileKey As Integer
  Dim strLine As String

  strDirectory = "C:\Documents and Settings\e1009028\My Documents\embosstest"

  ChDir (strDirectory)

  strFileName = Dir("*.txt")

  Do While Len(strFileName) > 0
    intFileKey = FreeFile
    Open strFileName For Input As intFileKey
    Do While Not EOF(intFileKey)
        Input #intFileKey, strLine
        MsgBox Mid(strLine, 1, 10)
    Loop
    strFileName = Dir
  Loop

End Sub

Here is a sample text file:

这是一个示例文本文件:

1 blahblahblabh
2 blah,blahblah

回答by Anders Lindahl

For a quick fix, try using Line inputinstead of input.

要快速修复,请尝试使用Line input而不是input

For a more modern solution, have a look at FileSystemObject, especially OpenTextFile.

对于更现代的解决方案,请查看FileSystemObject,尤其是OpenTextFile