在VBA中读取文件时分隔行

时间:2014-07-27 07:11:23

标签: excel vba excel-vba

我试图用VBA代码读取文件 - 但是当我尝试阅读它时,我将所有文本都作为一行:

    FileNum = FreeFile()
    Open FileName For Input As #FileNum

    While Not EOF(FileNum)
        Line Input #FileNum, DataLine    'Read The Data 1 Line At a Time
        If Len(DataLine) > 0 Then
            LineItems = Split(DataLine, vbTab)
        End If
    Wend

在这段代码中,我得到了" LineItems"中的所有文本,大约15K个单元格...... 我尝试使用" vbNewLine"而不是" vbTab"但后来我在一个单元格中得到了所有文本 - LineItems(0)(同样适用于使用" vbCrlf")

所以我的问题是 - 有没有人知道如何逐行取回文本?

如果没有 - 我想把文本分成几行,然后每行用Tabs将它分开...... 那么除了" vbNewLine"?

之外,还有其他新方法

谢谢!!

2 个答案:

答案 0 :(得分:0)

有回车符(13或0xd)(由vbscript和vba代码使用),有换行符(10或0xa)(由记事本用来忽略回归),并且有回车后跟换行符是传统的行结束(将打印头发送到行的开头并将纸张前进一行)。所有Windows程序都输入CR-LF对,但它们往往只读一个。

在Unix中,他们只存储依赖于阅读程序的LF来添加回归。这为每行存储节省了一个字节。

如果在32位窗口中打开文件,则以二进制模式编辑,显示字符。

edit /70 c:\windows\win.ini

♪◙是CR和LF的符号。不要将它们复制到这里,它们会被翻译成unicode。

答案 1 :(得分:0)

我在搜索中发现没有很好的解决方案。在我的问题中,我不得不读取Excel / VBA中来源不明的文件,因此可能是Unix / Mac或Windows风格的换行符。我的解决方案是创建一个解决问题的VBA类TextFileClass。这里是。它有效,但我不会彻底调试它。它仅适用于中等大小的文件,因为它们保存在内存中。它只有读写模式,但可以添加一个读写模式。

Option Explicit
Option Compare Text

Private fso As New FileSystemObject
Private file As TextStream
Private lines() As String
Private isOpenForWrite As Boolean

Public position As Integer
Public count As Integer

Public isOpen As Boolean
Public errMsg As String
Public lineSeparator As String

Function eof() As Boolean
    If position > UBound(lines) Then
        eof = True
    Else
        eof = False
    End If
End Function

Function fopen(fileName As String, mode As String) As Boolean
    Dim s As String

    errMsg = ""

    If mode = "r" Then
        isOpenForWrite = False
        Set file = fso.OpenTextFile(fileName, ForReading)
        s = file.ReadAll
        file.Close

        lines = Split(s, vbCrLf)
        If UBound(lines) > 1 Then
            lineSeparator = vbCrLf
        Else
            lines = Split(s, vbCr)
            If UBound(lines) > 1 Then
                lineSeparator = vbCr
            Else
                lines = Split(s, vbLf)
                If UBound(lines) > 1 Then
                    lineSeparator = vbLf
                Else
                    errMsg = "Line separator cannot be determined"
                    fopen = False
                    Exit Function
                End If
            End If
        End If

        position = 0
        count = UBound(lines) + 1
        fopen = True
        isOpen = True
    ElseIf mode = "w" Then
        isOpenForWrite = True
        Set file = fso.CreateTextFile(fileName, True)
        ReDim lines(0)
        lines(0) = ""
        fopen = True
        isOpen = True
        count = 0
        position = 0
    Else
        errMsg = "Mode not recognized"
        fopen = False
        isOpen = False
        isOpenForWrite = False
    End If
End Function

Function fclose() As Boolean
    Dim i As Integer

    If Not isOpen Then
        errMsg = "File is not open"
        fclose = False
        Exit Function
    End If
    fclose = True

    If isOpenForWrite Then
        For i = 0 To count - 1
            Call file.Write(lines(i) & lineSeparator)
        Next i

        file.Close
        Set file = Nothing
    End If

    errMsg = ""
    isOpen = False
    position = 0
    count = 0
    ReDim lines(0)
    lines(0) = ""
End Function

Function fread(ByRef line As String) As Boolean
    If Not isOpen Then
        errMsg = "File is not open"
        fread = False
        Exit Function
    ElseIf eof Then
        errMsg = "File is at eof"
        fread = False
        Exit Function
    End If

    fread = True
    errMsg = ""
    line = lines(position)
    position = position + 1
End Function

Function fwrite(line As String) As Boolean
    If Not isOpen Then
        errMsg = "File is not open"
        fwrite = False
        Exit Function
    End If

    Do While position >= count
        ReDim Preserve lines(count)
        count = count + 1
    Loop

    lines(position) = line
    position = position + 1
End Function