VBA将多个csv文件转换为制表符分隔

时间:2018-07-11 22:03:23

标签: excel vba excel-vba loops for-loop

我需要创建一个脚本,该脚本将以相同的名称保存选择的 .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 文件,但仅第一个保存。请帮忙!

1 个答案:

答案 0 :(得分:2)

您最好不要依赖任何 active 对象(在这种情况下为Workbook),而是引用适当的对象实例

另外,您应该通过将Application.DisplayAlerts设置为False来处理True,然后进入文件循环并在ExitHandler块中将其设置移回到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

并使用celebrities$newcol <- with(celebrities, age + income) 作为一般规则,以免发生任何溢出错误,因为整数最多可以达到32000 value or so