【问题标题】:Using VBA to open a tab delimited .txt file to save to .xlsx format使用 VBA 打开制表符分隔的 .txt 文件以保存为 .xlsx 格式
【发布时间】:2014-03-25 03:24:55
【问题描述】:

我正在尝试在 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

【问题讨论】:

  • 你试过Workbooks.OpenText吗?这似乎对我有用。试试Workbooks.OpenText Filename:="C:\filenameandpath", DataType:=xlDelimited, Tab:=True
  • 这是一个很好的开始,但之后我不知道如何将其保存为 xlsx。工作簿(复数)没有内置保存功能,仅适用于工作簿(单数)。
  • Excel 对单个工作簿的方法调用复数 Workbooks。它应该可以正常工作。
  • 我只看到您打开 1 个工作簿,因此 saveas 方法应该适合您。将 wb 调暗为工作簿 wb.SaveAs "fileNameHere", Excel.XlFileFormat.xlOpenXMLWorkbook
  • 您已经在使用Workbooks 打开您的文件。只需使用Set WB = Workbooks.OpenText Filename:=...,您就可以打开(一个)工作簿。要保存,您可以使用您现在使用的内容,也可以使用 FileFormat:=xlOpenXMLWorkbook 属性进行保存。

标签: vba excel xlsx


【解决方案1】:

让我们在这里有更多空间的地方用你自己的代码再试一次。尝试以下内容并阅读我上面的 cmets。我想你会发现它有效:

'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 语句。看起来您正在做完全相同的事情并使用相同的约定命名工作簿。你可能不需要它。我留下它是因为你没有具体询问它。你可以跳过它,只保存我认为的工作簿。

编辑:您需要 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 方法不返回对象,因此您必须在打开后使用Set WB = Workbooks(file) 设置WB 对象它假设file 是包含扩展名的完整文件名。我对那个不好。

【讨论】:

  • 输入文件可以是 .txt、.xls 或 .xlsx。我需要解析文件名以在文件扩展名之前添加前缀。它将始终保存为 .xlsx。这就是为什么我有 If,以及为什么我需要以不止一种方式打开。
  • 啊!我懂了。然后在打开工作簿之前需要 if 语句。看储蓄部分。它没有改变。一旦你打开工作簿,它就不会了。您需要查看文件名的末尾并设置一个 if 语句以使用 workbooks.OpenText 打开 .txt 和使用 Workbooks.Open 打开 .xls.xlsx。我将编辑我的答案。
  • 更新了顶部的代码,位于编辑行下方。包括 Dim 声明。您的行出现编译错误;可能是我的 Dim 和您的代码的组合。 (预期:语句结束 - 突出显示文件名)
  • 哪一行?你可能在那里有一个额外的逗号或其他东西。我从你最近的更新中看到的一切看起来都不错。语句的预期结尾只是告诉您该行的语法有问题。你有一个参数输入错误或什么的。
  • 找到了。 OpenText 不返回对象,因此 Set 在这里不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多