1

フォーマットされたデータを VBA にロードする最良の方法を探しています。fscanfC ライクまたは Fortran ライクな型関数に相当するものを探すのにかなりの時間を費やしましたが、成功しませんでした。

基本的に、テキストファイルから、それぞれ10個の数字を持つ多くの(100,000の)行に配置された何百万もの数字を読みたいと思います(最後の行、おそらく1〜10の数字を除く)。数値はスペースで区切られていますが、各フィールドの幅は事前にわかりません (この幅はデータ ブロック間で変化します)。例えば

  397143.1   396743.1   396343.1   395943.1   395543.1   395143.1   394743.1   394343.1   393943.1   393543.1

   -0.11    -0.10    -0.10    -0.10    -0.10    -0.09    -0.09    -0.09    -0.09    -0.09

 0.171  0.165  0.164  0.162  0.158  0.154  0.151  0.145  0.157  0.209 

以前はMidこの関数を使用していましたが、この場合は使用できません。各フィールドの幅が事前にわからないためです。また、Excel シートに読み込むには行数が多すぎます。連続する各文字を見て、それがスペースか数字かを判断する力ずくの方法を考えることができますが、それはひどく不器用に思えます.

フォーマットされたデータを書き込む方法についてのポインターにも興味がありますが、これは簡単に思えます。各文字列をフォーマットし、 を使用して連結するだけです&

4

2 に答える 2

4

次のスニペットは、テキストファイルから空白で区切られた数字を読み取ります。

Dim someNumber As Double

Open "YourDataFile.txt" For Input As #1

Do While Not (EOF(1))
    Input #1, someNumber
    `// do something with someNumber here...`
Loop

Close #1

更新:各行に可変数の項目を使用して、一度に1行を読み取る方法は次のとおりです。

Dim someNumber As Double
Dim startPosition As Long
Dim endPosition As Long
Dim temp As String

Open "YourDataFile" For Input As #1

Do While Not (EOF(1))
    startPosition = Seek(1)  '// capture the current file position'
    Line Input #1, temp      '// read an entire line'
    endPosition = Seek(1)    '// determine the end-of-line file position'
    Seek 1, startPosition    '// jump back to the beginning of the line'

    '// read numbers from the file until the end of the current line'
    Do While Not (EOF(1)) And (Seek(1) < endPosition)
        Input #1, someNumber
        '// do something with someNumber here...'
    Loop

Loop

Close #1
于 2009-06-25T14:03:58.983 に答える
2

正規表現を使用して複数の空白を1つのスペースに置き換えてから、以下に示すサンプルコードのように、各行に分割関数を使用することもできます。

65000行が処理された後、新しいシートがExcelブックに追加されるため、ソースファイルはExcelの最大行数よりも大きくなる可能性があります。

Dim rx As RegExp

Sub Start()

    Dim fso As FileSystemObject
    Dim stream As TextStream
    Dim originalLine As String
    Dim formattedLine As String
    Dim rowNr As Long
    Dim sht As Worksheet
    Dim shtCount As Long

    Const maxRows As Long = 65000

    Set fso = New FileSystemObject
    Set stream = fso.OpenTextFile("c:\data.txt", ForReading)

    rowNr = 1
    shtCount = 1

    Set sht = Worksheets.Add
    sht.Name = shtCount

    Do While Not stream.AtEndOfStream
        originalLine = stream.ReadLine
        formattedLine = ReformatLine(originalLine)
        If formattedLine <> "" Then
            WriteValues formattedLine, rowNr, sht
            rowNr = rowNr + 1
            If rowNr > maxRows Then
                rowNr = 1
                shtCount = shtCount + 1
                Set sht = Worksheets.Add
                sht.Name = shtCount
            End If
        End If
    Loop

End Sub


Function ReformatLine(line As String) As String

    Set rx = New RegExp

    With rx
        .MultiLine = False
        .Global = True
        .IgnoreCase = True
        .Pattern = "[\s]+"
        ReformatLine = .Replace(line, " ")
    End With

End Function


Function WriteValues(formattedLine As String, rowNr As Long, sht As Worksheet)

    Dim colNr As Long
    colNr = 1

    stringArray = Split(formattedLine, " ")
    For Each stringItem In stringArray
        sht.Cells(rowNr, colNr) = stringItem
        colNr = colNr + 1
    Next

End Function
于 2009-06-25T14:34:05.633 に答える