VBS和宏之间QueryTables.add的差异

时间:2019-02-20 14:06:45

标签: vba vbscript

我有一个可将平面文件加载到当前工作表中的宏。它在子文件夹中搜索与活动工作表同名的.dat文件,并加载数据。我想将其转换为vbs脚本,该脚本将遍历工作簿中的所有工作表并导入所有数据。我无法使用宏,因为打开工作簿并尝试执行此操作时,excel内存不足。下面是宏:

Error   LNK2019 unresolved external symbol SQLGetDiagRec referenced in function "void __cdecl showSQLError(unsigned int,void * const &)" (?showSQLError@@YAXIAEBQEAX@Z) C:\Users\uib02930\Documents\Visual Studio 2015\Projects\cmaketest\CMakeLists.txt    C:\Users\uib02930\Documents\Visual Studio 2015\Projects\cmaketest\main.obj  1   
Error   LNK1120 1 unresolved externals  C:\Users\uib02930\Documents\Visual Studio 2015\Projects\cmaketest\CMakeLists.txt    

下面是我尝试运行的VBScript:

Sub LoadData()
Dim xStrPath As String
Dim theSheet As Worksheet
Dim xFile As String
Dim xCount As Long
Dim oneCell As Range

    answer = MsgBox("Are you sure you want to reload data? This will remove all existing data.", vbYesNo + vbQuestion, "Warning!")
    If answer = vbYes Then
            Set theSheet = Application.ActiveWorkbook.ActiveSheet
            theSheet.Rows(5 & ":" & theSheet.Rows.Count).Delete
            xStrPath = Application.ActiveWorkbook.Path
            Application.ScreenUpdating = False
            xFile = xStrPath & "\Old_Data\" & theSheet.Name & ".dat"
                With theSheet.QueryTables.Add(Connection:="TEXT;" _
                  & xStrPath & "\Old_Data\" & theSheet.Name & ".dat", Destination:=theSheet.Range("A5"))
                    .Name = "a" & xCount
                    .FieldNames = True
                    .RowNumbers = False
                    .FillAdjacentFormulas = False
                    .PreserveFormatting = True
                    .RefreshOnFileOpen = False
                    .RefreshStyle = xlInsertDeleteCells
                    .SavePassword = False
                    .SaveData = True
                    .AdjustColumnWidth = True
                    .RefreshPeriod = 0
                    .TextFilePromptOnRefresh = False
                    .TextFilePlatform = xlMSDOS
                    .TextFileStartRow = 1
                    .TextFileParseType = xlDelimited
                    .TextFileTextQualifier = xlTextQualifierDoubleQuote
                    .TextFileConsecutiveDelimiter = False
                    .TextFileTabDelimiter = False
                    .TextFileSemicolonDelimiter = False
                    .TextFileCommaDelimiter = False
                    .TextFileSpaceDelimiter = False
                    .TextFileOtherDelimiter = ","
                    .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
                    .TextFileTrailingMinusNumbers = True
                    .Refresh BackgroundQuery:=False
                End With

                For Each oneColumn In theSheet.UsedRange.Columns
                    With oneColumn
                        .ColumnWidth = 40
                    End With
                Next oneColumn
    Else
    'do nothing
    End If
End Sub

VBScript在第18行char 49上给了我一个预期的')'错误:

Error

此行是

    Dim xStrPath
Dim theSheet
Dim xFile
Dim xCount
Dim oneCell
Dim theFile
Dim WshShell

Set WshShell = CreateObject("WScript.Shell")
Set xStrPath = WshShell.CurrentDirectory
Set theFile = GetObject(xStrPath & "\Base_Tables_Template.xlsm")

For Each theSheet In theFile

    'Set theSheet = Application.ActiveWorkbook.ActiveSheet
    theSheet.Rows(5 & ":" & theSheet.Rows.Count).Delete
    xFile = xStrPath & "\Old_Data\" & theSheet.Name & ".dat"
        With theSheet.QueryTables.Add(Connection:="TEXT;" _ & xStrPath & "\Old_Data\" & theSheet.Name & ".dat", Destination:=theSheet.Range("A5"))
            .Name = "a" & xCount
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = xlMSDOS
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = ","
            .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With

        For Each oneColumn In theSheet.UsedRange.Columns
            With oneColumn
                .ColumnWidth = 40
            End With
        Next oneColumn
Next theSheet

它的行为应与宏相同。当宏执行得很好时,为什么代码需要括号呢?

0 个答案:

没有答案