开发者

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

开发者 https://www.devze.com 2023-02-27 00:42 出处:网络
I have a simple Excel 2007 Macro that is reading a text file line by line and displaying the output.

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

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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消