【问题标题】:Convert multiple csv files to tab delimited将多个 csv 文件转换为制表符分隔
【发布时间】:2018-07-11 22:03:22
【问题描述】:

我需要将选择的 .csv 文件以相同的名称保存为 制表符分隔 文件。

我有如下代码选择文件,指定分隔符,然后遍历文件,一个一个打开保存。

问题在于迭代。

Sub sandbox2()

    Dim xFilesToOpen As Variant
    Dim i As Integer
    Dim xWb As Workbook
    Dim xTempWb As Workbook
    Dim xDelimiter As String
    Dim xScreen As Boolean
    Dim strFileFullName As String

    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False

    xFilesToOpen = Application.GetOpenFilename("Text Files (*.*), *.*", , , , True)
    xDelimiter = Application.InputBox("Delimiter: ", , ",")

    If TypeName(xFilesToOpen) = "Boolean" Then
        MsgBox "No files were selected", , "Sorry dude"
        GoTo ExitHandler
    End If

    For i = 1 To UBound(xFilesToOpen)
        Set xTempWb = Workbooks.Open(xFilesToOpen(i))
        strFileFullName = Replace(ActiveWorkbook.FullName, ".csv", "")
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:=strFileFullName + ".txt", FileFormat:=xlText, CreateBackup:=False
        xTempWb.Close False
        Application.DisplayAlerts = True
        ActiveWorkbook.Close savechanges:=False
        Set xTempWb = Nothing
    Next i

ExitHandler:
    Application.ScreenUpdating = xScreen
    Set xWb = Nothing
    Set xTempWb = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description, , "error"
    Resume ExitHandler
End Sub

此代码适用于第一个文件,但随后出现错误

“对象变量或未设置块变量”

查看文件夹时,第一个文件保存为.txt文件,但只有第一个。

【问题讨论】:

  • 当您之前已经使用xTempWb.Close False 关闭了工作簿时,ActiveWorkbook.Close savechanges:=False 行在循环中做了什么?另外我会将ActiveWorkbook.SaveAs 替换为xTempWb.SaveAs,因为引用活动工作簿不可靠。如果没有帮助,请不要让我们猜测并说明错误出现在哪一行。

标签: excel vba loops for-loop


【解决方案1】:

您最好不要依赖任何活动对象(在本例中为Workbook),而是引用一个适当的对象实例

另外,您应该通过将Application.DisplayAlerts 设置为False 来处理False 在进入文件循环之前 并将其设置移回ExitHandler 块中的True,并确保始终将其设置为出现任何错误

所以我会如下:

Sub sandbox2()

    Dim xFilesToOpen As Variant
    Dim i As Long
    Dim xScreen As Boolean

    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False

    xFilesToOpen = Application.GetOpenFilename("Text Files (*.*), *.*", , , , True)
    xDelimiter = Application.InputBox("Delimiter: ", , ",")

    If TypeName(xFilesToOpen) = "Boolean" Then
        MsgBox "No files were selected", , "Sorry dude"
    Else
        Application.DisplayAlerts = False ' set it to 'False' only once before entering the loop
        For i = 1 To UBound(xFilesToOpen)
            With Workbooks.Open(xFilesToOpen(i)) ' open a workbook with current name and reference it
                .SaveAs Filename:=Replace(.FullName, ".csv", "") + ".txt", FileFormat:=xlText, CreateBackup:=False ' act on referenced workbook properties
                .Close False ' close referenced workbook
            End With
        Next i
    End If

ExitHandler:
    Application.DisplayAlerts = True ' be sure to set it back to 'True' should any error take you out of your loop
    Application.ScreenUpdating = xScreen
    Exit Sub
ErrHandler:
    MsgBox Err.Description, , "error"
    Resume ExitHandler
End Sub

请注意我摆脱了

Dim xTempWb As Workbook ' no longer used

Dim xWb As Workbook ' never used
....
Set xWb = Nothing ' never set

Dim xDelimiter As String ' never used
....
xDelimiter = Application.InputBox("Delimiter: ", , ",") ' never used

并使用Dim i as Long,作为一般规则不会引发任何溢出错误,因为整数可以达到一些32000 value or so

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多