VBA脚本运行时错误

时间:2016-06-01 12:04:47

标签: excel vbscript

执行我的VBA脚本时出错

消息是:

  

第3行   Char 12
  错误预期声明结束
  代码800A0401
  源MS VBScript编译错误

这是我的脚本,我想要做的是将TXT转换为XLS

Sub TXTconvertXLS()
    'Variables
    Dim wb As Workbook
    Dim strFile As String
    Dim strDir As String

    'Directories
    strDir = "X:\X\X\X\xxxx\"
    strFile = Dir(strDir & "*.txt")

    'Loop
    Do While strFile <> ""
        Set wb = Workbooks.Open(strDir & strFile)
            With wb
                .SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
                .Close True
            End With
        Set wb = Nothing
    Loop
 End Sub

1 个答案:

答案 0 :(得分:0)

使用Dim时无需初始化变量类型:

Sub TXTconvertXLS()
    'Variables
    Dim wb
    Dim strFile
    Dim strDir

    'Directories
    strDir = "X:\X\X\X\xxxx\"
    strFile = Dir(strDir & "*.txt")

    'Loop
    Do While strFile <> ""
        Set wb = Workbooks.Open(strDir & strFile)
            With wb
                .SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
                .Close True
            End With
        Set wb = Nothing
    Loop
End Sub