使用VBA打开制表符分隔的.txt文件以保存为.xlsx格式

时间:2014-03-24 17:50:33

标签: excel vba excel-vba xlsx

我尝试在Excel中使用VBA来自动将.txt文件(制表符分隔)转换为.xlsx文件。这就是我所拥有的:

Set WB = Workbooks.Open(folder + file, , , 1)
If Right(file, 3) = "txt" Or Right(file, 3) = "xls" Then
    Application.DisplayAlerts = False
    WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 4) + "_" + metricDate + "_.xlsx", _
        FileFormat:=51
    Application.DisplayAlerts = True
Else
    Application.DisplayAlerts = False
    WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 5) + "_" + metricDate + "_.xlsx", _
        FileFormat:=51
    Application.DisplayAlerts = True
End If
WB.Close

当然,这只是一段代码,我认为代码的第一部分是最相关的。我只是开始检查转换后的.txt文件,因为它们是保存后应该是10%的大小。事实证明,二十列被压成三列,所有空格和标签都被删除了。不知道发生了什么,因为我不经常使用VBA。

我认为关键在于:

Set WB = Workbooks.Open(folder + file, , , 1)

最后的1表示制表符分隔。不确定它会对它打开的.xls文件做什么,但我会担心下一步。

感谢您提供的任何指示。


编辑。

我改变了代码来区别对待.txt和.xls,就像我本来应该做的那样。这是当前的代码:

Dim WB As Workbook
'Dim WBS As Workbooks

If Right(file, 3) = "txt" Then
    Set WB = Workbooks.OpenText Filename:=folder + file, DataType:=xlDelimited, Tab:=True
    Application.DisplayAlerts = False
    WB(1).SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 4) + "_" + metricDate + "_.xlsx", _
        FileFormat:=51
    Application.DisplayAlerts = True
    WB.Close
ElseIf Right(file, 3) = "xls" Then
    Set WB = Workbooks.Open(folder + file)
    Application.DisplayAlerts = False
    WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 4) + "_" + metricDate + "_.xlsx", _
        FileFormat:=51
    Application.DisplayAlerts = True
    WB.Close
Else
    Set WB = Workbooks.Open(folder + file)
    Application.DisplayAlerts = False
    WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 5) + "_" + metricDate + "_.xlsx", _
        FileFormat:=51
    Application.DisplayAlerts = True
    WB.Close
End If

1 个答案:

答案 0 :(得分:0)

让我们再次尝试使用您自己的代码,这里我有更多的空间。尝试以下内容并阅读上面的评论。我想你会发现它有效:

'I'm adding this line.  I'm assuming you have it in your code, but just to be certain...
Dim WB As Excel.Workbook
'This line opens your tab delimeted text file.
Set WB = Workbooks.OpenText(Filename:=folder + file, DataType:=xlDelimited, Tab:=True
If Right(file, 3) = "txt" Or Right(file, 3) = "xls" Then
    'This section turns off alerts, saves the workbook opened in the previous step as xlsx and turns alerts back on.
    Application.DisplayAlerts = False
    WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 4) + "_" + metricDate + "_.xlsx", FileFormat:=xlOpenXMLWorkbook
    Application.DisplayAlerts = True
Else
    'Again, this section saves the workbook opened in the previous step as xlsx.
    Application.DisplayAlerts = False
    WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 5) + "_" + metricDate + "_.xlsx", FileFormat:=xlOpenXMLWorkbook
    Application.DisplayAlerts = True
End If
WB.Close

我也在辩论你是否真的需要那句话。看起来你正在做同样的事情,并使用相同的约定命名工作簿。你可能不需要它。我离开了,因为你没有具体询问它。您可以跳过它,只保存我认为的工作簿。

编辑:您需要使用If语句来选择用于打开工作簿的方法...

'I'm adding this line.  I'm assuming you have it in your code, but just to be certain...
Dim WB As Excel.Workbook
If Right(file, 3) = "txt" then
    'This line opens your tab delimeted text file.
    Set WB = Workbooks.OpenText(Filename:=folder + file, DataType:=xlDelimited, Tab:=True
Else
    'This line opens your xls and xlsx books
    Set WB = Workbooks.Open(folder + file) 'no additional parameters should be needed
End If
Application.DisplayAlerts = False
WB.SaveAs filename:=folder + milestone + "_" + loadtype + "_" + Left(file, Len(file) - 4) + "_" + metricDate + "_.xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
WB.Close

如果您正在迭代许多这些输入工作簿,那么您可能想要

Set WB = Nothing

只是为了安全。

编辑:我会让我的耻辱在那里... OpenText方法不返回对象,所以你必须使用{{1}设置WB对象在您打开它之后,假设Set WB = Workbooks(file)是包含扩展名的完整文件名。我对那个不好。