VBA导入的文本文件分隔到Excel

时间:2017-03-15 09:32:13

标签: excel-vba text-files delimiter vba excel

我正在尝试使用vba将文本文件导入excel。我拥有的文本文件包含一行中的所有数据,并包含两个分隔符“|”和“,”。其中,“,”将数据分成列和“|”将数据分隔为行。

我有一个代码,但似乎正好相反,因为我对vba很新,我无法弄清楚它出错的地方。

我在想是否有其他方法可以执行vba,如果它识别分隔符会将数据移动到指定的单元格,那么会执行某些操作?

这是我的文本文件的样子。

27/2/2017 17:14:32 | 54,11,6,32,58,83,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,69,8,86,0,241,255 | 0,71,69,404,0,553,0 | 15,0,0,0,53,0,0 | 0,0,0,0,0,0,0 | 0,867,2,18,0,939,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 16,0,0,0,0,0,85 | 647,509,18,82,18,670,85 | 1433,0,0,0,0,0,0 | 1432,882,0,0,0,939,0 | 32,861,1,20,0,938,0 | 0,887,0,0,0,939,0 | 0,886,0,0,0,939,0 | 12,801,4,42,0,912,0 | 0,867,0,0,0,939,0 | 0,0,0,0,0,0,0 | 0,890,0,0,0,939,0 | 0,871,0,0,0,930,85 | 0,891,0,0,0,939,0 | 0,892,0,0,0,939,0 | 0,894,0,0,0,939,0 | 0,895,0,0,0,954,0 | 0,0,0,0,0,0,0 | 0,905,0,0,0,954,0 | 0,792,6,35,0,897,85 | 4,697,40,202,0,952,0 | 0,640,13,108,0,807,0 | 0,0,0,0,507,0,0 | 60,24,23,211,1128,296,0 | 4,81,16,148,569,348,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 77,224,22,367,159,791,0 | 118,42,1,7,1051,104,0 | 58,0,0,0,654,0,0 | 260,0,0,0,642,0,0 | 172,0,0,0,1241,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1434,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1434,0,0 | 0,0,0,0,1433,0,0 | 114,0,0,0,1284,0,0 | 0,0,0,0,1429,0,0 | 0,0,0,0,1353,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1434,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1432,0,0 | 0,0,0,0,1434,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 86,89,1,51,1279,141,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1434,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1433,0,0 | 0,0,0,0,1434,0,0 | 0,0,0,612,751,613,0 | 0,0,2,662,0,710,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,0,0 | 0,0,0,0,0,3,0 | 

这是我的代码

Private Sub CommandButton1_Click()

Dim sPath As String, sLine As String
Dim oFile As String
Dim i As Long
Dim workRange As Range
Dim destCell As Range


Set destCell = Range("A1")
Set workRange = Range("A1" & ":" & Range("A1").End(xlDown).Address)

Unload Me
oFile = Application.GetOpenFilename()

i = 1

Open oFile For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
    Input #1, sLine ' Read data

    i = i + 1
    Range("A" & i).Formula = sLine  ' Write data line


Loop
Close #1 ' Close file.


'Text to Columns
    With workRange
    .TextToColumns Destination:=destCell, DataType:=xlDelimited, _
     TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
     Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="|", _
     FieldInfo:=Array(1, 1), TrailingMinusNumbers:=False
    End With

Application.ScreenUpdating = True
End Sub

我希望结果看起来像这样

enter image description here

2 个答案:

答案 0 :(得分:1)

修改后的代码使其更加清晰,现在正在运行。请参阅下面的代码。

Private Sub CommandButton1_Click()

Dim text As String, textline As String
Dim Cell As Range
Dim strLine() As String
Dim aCol, aRow, aNames, colNow As Long, rowNow As Long
Dim oldDate As Date, newDate As Date


Unload Me
myFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")

If myFile = False Then
Exit Sub
'MsgBox ("No File Select. Exit")

Else

'Open and read file
Open myFile For Input As #1
    Do Until EOF(1)
        Line Input #1, textline
        text = text & textline
    Loop
Close #1

aRow = Split(textline, "|")
With ActiveSheet

    .Cells(2, 1) = Trim(Left(aRow(0), InStr(aRow(0), " ")))

    For rowNow = 1 To UBound(aRow)

        aCol = Split(aRow(rowNow), ",")
        For colNow = 0 To UBound(aCol)
            ActiveSheet.Cells(rowNow + 1, colNow + 2) = aCol(colNow)
        Next
    Next

End With
End If
End Sub

答案 1 :(得分:0)

编辑删除日期异议并声明我的变量

Dim aCol, aRow, aNames, colNow As Long, rowNow As Long, sLine As String
'Text to Columns
aRow = Split(sLine, "|")
With ActiveSheet
    .Cells(2, 1) = Trim(Left(aRow(0), InStr(aRow(0), " ")))
    '.Cells(2, 1) = Format(DateValue(aRow(0)), "d/m/yy")
    For rowNow = 1 To UBound(aRow)
        aCol = Split(aRow(rowNow), ",")
        For colNow = 0 To UBound(aCol)
            Sheet1.Cells(rowNow + 1, colNow + 2) = aCol(colNow)
        Next
    Next
End With

哦,编辑添加列标题:

aNames = Array("Date and Time", "John", "Kate", "Sean", "Stephen", "Brian", "Philip", "Peter")
For colNow = 0 To UBound(aNames)
    ActiveSheet.Cells(1, colNow + 1) = aNames(colNow)
Next