在VBA中使用OpenText将分隔的TXT转换为XLS?

时间:2017-05-10 09:44:54

标签: excel-vba vba excel

我想将分隔的txt转换为xls文件。我想出了两个非常简单的命令行,我认为这些命令应该可以工作,但实际上它并没有。谁能告诉我我犯了什么错误?

如果这项工作,我将使用dir为3000 txt文件循环...

谢谢!

博斯科

Sub ConvertDelimitedTXTtoXLS()
    Dim wb As Workbooks
    wb.OpenText Filename:="C:\Users\boscotsin\Desktop\test0.txt", DataType:=xlDelimited, Tab:=True
    wb.SaveAs Filename:="C:\Users\boscotsin\Desktop\success.xls"
End Sub

2 个答案:

答案 0 :(得分:0)

Sub delimitedTXTtoXLS()
    Dim wb As Workbook
    Workbooks.OpenText filename:="C:\Users\boscotsin\Desktop\test0.txt", origin:=xlMSDOS, DataType:=xlDelimited, Tab:=True
    Set wb = ActiveWorkbook
    wb.SaveAs filename:="C:\Users\boscotsin\Desktop\success.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    wb.Close SaveChanges:=True
End Sub

答案 1 :(得分:0)

' Excel8

Sub delimitedTXTtoXLS2()
    Dim wb As Workbook
    Workbooks.OpenText FileName:="C:\Users\boscotsin\Desktop\test0.txt", origin:=xlMSDOS, DataType:=xlDelimited ', Comma:=True
    Set wb = ActiveWorkbook
    wb.SaveAs FileName:="C:\Users\boscotsin\Desktop\success.xlsx", FileFormat:=56, CreateBackup:=False
    wb.Close SaveChanges:=True
End Sub