首页 > 解决方案 > 查找输入文件的行尾标记

问题描述

我有一个宏可以读取输入文件,一次一个字符。我需要它来找到文件的行尾标记。这是我到目前为止所拥有的:

While Not EOF(inFileNum)
       oneChar = Input(1, #inFileNum)
       While (oneChar <> vbLf)

标签: excelvbaeol

解决方案


像这样的东西:

Dim handle As Long, ch, sep, i

handle = FreeFile
Open "D:\Stuff\test.csv" For Input As handle
Do While Not EOF(handle)
    ch = Input(handle, 1)
    If Len(sep) = 0 Then
        If ch = vbLf Or ch = vbCr Then sep = ch
    Else
        'vbCr + vbLf is a valid combination,
        '  otherwise it should be a single character....
        If sep = vbCr And ch = vbLf Then sep = vbCrLf
        Exit Do
    End If
Loop
Close handle

If Len(sep) > 0 Then Debug.Print "Found line separator:"
For i = 1 To Len(sep)
    Debug.Print Asc(Mid(sep, i, 1))
Next i

推荐阅读