Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列标题和数据值始终右对齐时导入Excel txt文件_Excel_Vba_Import_Text Files - Fatal编程技术网

列标题和数据值始终右对齐时导入Excel txt文件

列标题和数据值始终右对齐时导入Excel txt文件,excel,vba,import,text-files,Excel,Vba,Import,Text Files,我有一个很大的txt文件,它由一个矩阵组成,其中列标题和数据值总是右对齐的。这是我能找到的唯一允许将txt导入excel的“常规功能”,因为元素之间由不规则的空格分隔,这取决于数据编号的大小 矩阵在多个位置有空单元格。看起来是这样的: 4536 4705 2360 2355 2717 56099

我有一个很大的txt文件,它由一个矩阵组成,其中列标题和数据值总是右对齐的。这是我能找到的唯一允许将txt导入excel的“常规功能”,因为元素之间由不规则的空格分隔,这取决于数据编号的大小

矩阵在多个位置有空单元格。看起来是这样的:

              4536        4705        2360        2355        2717       
 56099                                                      5156.5 
 12470      4114.9                                                                 
 12469                    23.0                                          
 56997                  2311.9                                             
 12471                                                                 
 12479                                                                 
 12473                                                                 
 12478        41.1                                                               
 12484                                         78957.7                      
 12477                                                                 
 12483                                                                 
 12476                           7125444.9                                     

对如何使用VBA进行导入有何建议?我怎样才能要求与右侧对齐的条件?谢谢。

您文章中的数据与列宽12对齐

此宏将打开txt文件
data2
,其中包含帖子中的信息,并适当设置列

Sub OpenMatrixData()
    Dim filename As String
    filename = "C:\Test\data2.txt"
    Dim firstColumnWidth As Integer
    Dim columnWidth As Integer

    firstColumnWidth = 6
    columnWidth = 12

    Workbooks.OpenText filename:=filename, Origin:=437, StartRow:= _
        1, _
        DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), _
        Array(firstColumnWidth, 1), _
        Array(firstColumnWidth + columnWidth * 1, 1), _
        Array(firstColumnWidth + columnWidth * 2, 1), _
        Array(firstColumnWidth + columnWidth * 3, 1), _
        Array(firstColumnWidth + columnWidth * 4, 1), _
        Array(firstColumnWidth + columnWidth * 5, 1)), _
        TrailingMinusNumbers:=True
End Sub
Excel对齐:

|    |  4536|  4705|     2360|   2355|   2717|
56099|      |      |         |       | 5156.5|
12470|4114.9|      |         |       |       |
12469|      |    23|         |       |       |
56997|      |2311.9|         |       |       |
12471|      |      |         |       |       |
12479|      |      |         |       |       |
12473|      |      |         |       |       |
12478|  41.1|      |         |       |       |
12484|      |      |         |78957.7|       |
12477|      |      |         |       |       |
12483|      |      |         |       |       |
12476|      |      |7125444.9|       |       |
回复OP评论:

对于动态列数,您可以尝试一下,它依赖于
Excel
来确定如何处理数据。它与示例一起工作,但不如上面的解决方案那么健壮

Sub OpenMatrixData()
    Dim filename As String
    filename = "C:\Test\data2.txt"

    Workbooks.OpenText filename:=filename
End Sub

谢谢。它适用于示例,但原始数据有500多列。如何为n列循环宏?@user12975当你陈述整个问题时,它会有所帮助。当你发布你的代码时会更好。重述您的问题,将变量列包括在内,然后我将了解有关解决方案的信息。:)@user12975更新了我的解决方案。