0

我有块格式的 .txt 数据:

1
1
2
2
3
3

在这个例子中,我有 3 个大小为 2 的块。大小不随块而变化,它是固定的。我的真实案例有大约 500 个大小为 500 的块。我想在 Excel 中以格式导入此类数据

1 2 3
1 2 3

即每一列代表一个块。我正在寻找一个在线工具,它可以为此提供简单的复制到剪贴板工具,但我没有找到。考虑到我的列的范围和块的大小(此处为范围 A1:A6 和块大小 2),您将如何在 Excel VBA 中执行此操作?

4

1 回答 1

1
Sub Tester()
    ReadBlocks "C:\local files\tmp.txt", 100, ActiveSheet.Range("a1")
End Sub


Sub ReadBlocks(sPath As String, BlockSize As Long, rngDest As Range)

    Dim fso As Object, f As Object, val, r As Long, c As Long

    On Error GoTo haveError

    Set fso = CreateObject("scripting.filesystemobject")
    Set f = fso.opentextfile(sPath, 1) '1=forReading

    r = 0
    c = 0
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Do Until f.AtEndOfStream
        rngDest.Offset(r, c).Value = f.ReadLine
        If (r + 1) Mod BlockSize = 0 Then
            r = 0
            c = c + 1
        Else
            r = r + 1
        End If
    Loop

haveError:

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub
于 2013-04-05T18:09:37.770 回答