用于将特定数据文件数据转换/导入到XbyY excel表单的VBA代码

时间:2017-12-10 16:58:00

标签: excel vba excel-vba

数据文件包含16行,每行16个字符。 想要将这些数据放入16x16 excel表中。

下面的示例代码(来自此处的一个帖子),但没有" |"使用的字符,所以不知道需要改变什么来完成任务。

VBA code snippet from previous Q asked

下面的数据文件内容示例。 的。 (句号)表示在单元格列中出现的空白。

.2A.....5C.9..6G

6..G.5471A82.D..

.8...B2...E7...A

....6AE..4GF3.87

1E7.C8..G.46FBA9

.D.....4.E.3.CG.

.F.C.2A571B.683E

A.5..9...2.C7.D.

..3.A..9.8.4.EBD

..E6.G1D.....3..

C.....7..D1..A4.

DA.....63.7EC...

3...F.9....85G..

........EB..DF98

EB.A5..2...1....

F.9.B.8..75GA.2.

感谢您的帮助......

1 个答案:

答案 0 :(得分:0)

我只想尝试:

Option Explicit

Sub Read_and_Save_XbyY()
    Dim path As String
    Dim fileName As String
    Dim excelName As String
    Dim parts() As String
    Dim wbData As Workbook
    Dim wsData As Worksheet

    path = "D:\temp\X_by_Y\"
    fileName = "MyData.txt"

    ' Prepare filenames
    parts = Split(fileName, ".")
    excelName = parts(LBound(parts)) + ".xlsx"

    ' Open inputfile
    Workbooks.OpenText (path + fileName)
    Set wbData = Workbooks(fileName)
    Set wsData = wbData.Worksheets(1)

    ' Split into 16 columns
        wsData.[A1:A16].TextToColumns _
            Destination:=[A1], _
            DataType:=xlFixedWidth, _
            FieldInfo:=Array(Array(0, 2), Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2), _
                          Array(5, 2), Array(6, 2), Array(7, 2), Array(8, 2), Array(9, 2), _
                          Array(10, 2), Array(11, 2), Array(12, 2), Array(13, 2), _
                          Array(14, 2), Array(15, 2))

    ' Save xlsx file
    wbData.SaveAs fileName:=path + excelName, FileFormat:=xlOpenXMLWorkbook

    ' Close data workbook
    wbData.Close

End Sub