2017-03-15 68 views
1

我想使用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

+0

你为什么不只是更换'。这将使它更容易。 –

+0

@Peh我无法替换,因为文本文件是在其他设备中生成的。 – rei123

回答

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 
+0

嗨,谢谢你的回复。感谢您的答复。我在这一行上得到一个类型mismathc错误:.Cells(2,1)= Format(DateValue(aRow(0)),“d/m/yy”) – rei123

+0

@Peh,你是对的,你错了。您有权突出显示日期问题,但我的本地日期格式为日/月/年,数据中的日期为“2017/2/27 17:14:32”。所以我的日期计算适用于我,也适合数据,这是d/m/y。如果数据是y/m/d或m/d/y,我会用不同的方式计算日期。顺便说一下,DATESERIAL不是万能的,因为它取决于先前的知识 - 即日期的哪一部分是年份,哪一个月,哪一天。如果数据日期的格式会有所不同,那么总有一天你会最终把这一年塞进一天! – Winterknell

+0

无论如何,我在这里选择的解决方案只是简单地从时间戳中提取时间,并显示原始日期,OP可以按照他的选择进行按摩。其余的真的取决于学生! – Winterknell

0

修改代码,使其更清晰,其现在的工作。见下面的代码。带有换行符导入该文件之前`|

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 
相关问题