将文本文件打印到Excel工作表

将文本文件打印到Excel工作表,excel,vba,Excel,Vba,我有一个名为“amk.txt”的文本文件,里面看起来像: Test Number 1234 sampleCounter 123 Time Speed[km\h] 1 12 2 13 3 14 4 15 我需要使用V

我有一个名为“amk.txt”的文本文件,里面看起来像:

Test Number 1234

sampleCounter 123

Time                       Speed[km\h]



1                             12

2                             13

3                             14

4                             15
我需要使用VBA将内容打印到Excel表格。我有一个读取文件内容并将内容保存到数组中的函数。阵列内部看起来如下所示:

TestNumber1234

sampleCounter123

TimeSpeed[km\h]



112

213

314

415
我的问题是,保存文件内容的数组看起来不像txt文件的内部。 因此,我必须回答以下问题:

  • 将文件内容保存到数组或直接打印到excel工作表中是否有意义

  • 如果要将其保存到数组中,为什么数组看起来不像文本文件

  • 我编写了两个不同的函数将文件内容保存到数组中

    第一个功能:

    Public Function read_file_with_FSO(fileName)
    Const ForReading=1
    Set fileObject = CreateObject("Scripting.FileSystemObject")
    Set file= fileObject.OpenTextFile(fileName, ForReading)
    fileContentFSO=Split(f.readAll,vbNewline)
    read_file_with_FSO=fileContentFSO
    End Function
    
    Public Function read_file(fileName)
    index=0
    Open fileName For Input as #1
    Line Input #1, textline
    fileContent(index)=textline
    index=index+1
    Loop
    Close #1
    
    read_file=fileContent
    End Function
    
    第二个功能:

    Public Function read_file_with_FSO(fileName)
    Const ForReading=1
    Set fileObject = CreateObject("Scripting.FileSystemObject")
    Set file= fileObject.OpenTextFile(fileName, ForReading)
    fileContentFSO=Split(f.readAll,vbNewline)
    read_file_with_FSO=fileContentFSO
    End Function
    
    Public Function read_file(fileName)
    index=0
    Open fileName For Input as #1
    Line Input #1, textline
    fileContent(index)=textline
    index=index+1
    Loop
    Close #1
    
    read_file=fileContent
    End Function
    

    您拥有的数组是文件中的每一行

    由于它是一个带有制表符的文本文件,所以您可以将内容全部读取并粘贴到单元格A1中,并且由于制表符,它将跨多行和制表符分隔的列填充

    Range("A1").Select 
    ActiveSheet.Paste
    

    您可以用谷歌搜索如何使用VBA剪贴板,保存剪贴板中已有的内容,将文件内容设置为剪贴板,然后执行上面的“粘贴”命令,最后恢复用户以前在剪贴板中拥有的内容。


    第二个屏幕截图类似于VBEditor监视窗口,它将每一行显示为一个数组项。您可以迭代数组,使用
    Split
    获取元素并在嵌套循环中对其进行布局。

    尝试以下代码将文件加载到excel:

    Sub Load_text_file()
    
    Dim strFilename As String, strLine As String, strSprt As String
    Dim lngRows As Long
    
    strFilename = Application.GetOpenFilename("Text files, *.txt") ' you may replace this with direct path to your file. NOTE there is no handler for "Cancel" button here - so there will be an error if you press it
    
    Open strFilename For Input As #1
    
    lngRows = 1
    
    Do While Not EOF(1)
        Line Input #1, strLine
        ActiveSheet.Cells(lngRows, 1) = strLine ' !!! replace an ActiveSheet with your sheet's name
    
        lngRows = lngRows + 1
    Loop
    
    Close #1
    
    End Sub
    
    然后转到Excel,选择复制的数据,转到“数据”选项卡,按“文本到列”按钮。在第一步中,选择“Delimited”,然后按next。在第二步中,请尝试选择不同的分隔符,除非您看到Excel使用以下分隔符之一将文本拆分为列:

    如果在文件中找到分隔符(通常是制表符、逗号或分号),则可以将以下代码添加到上面的子项中:

    With ActiveSheet ' !!! replace an ActiveSheet with your sheet's name
        range(.Cells(1, 1), .Cells(lngRows - 1, 1)).TextToColumns Destination:=.Cells(1, 1), Tab:=True ' In my case the delimiter is Tab, so set it to true, when you start typing editor will suggest parameters. If you'd like to use other character - you will need to set also that char:  other:=True, otherchar:="|"
    End With
    
    如果这没有帮助-您可能需要更改文本文件格式或创建一些子文件或函数,以便在Excel中正确设置文本格式

    尝试代码,阅读注释,询问是否有不清楚的地方