我可以用常量替换Array吗?

时间:2013-11-22 09:15:26

标签: sql excel vba

我在Excel中使用VBA进行查询,我想要替换

St" _
            ), Array( _
            "efl\zv162_part1.xls

常量 part1

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DSN=Excel Files;DBQ=\\St" _
        ), Array( _
        "efl\zv162_part1.xls;DefaultDir=cesta;DriverId=1046;FIL=excel 12.0;MaxBufferSize=2048;PageTimeout=5;" _
        )), Destination:=Range("$A$1")).QueryTable
        .CommandText = Array( _
        "SELECT `zv162_part1$`.F2, `zv162_part1$`.F4, `zv162_part1$`.F5, `zv162_part1$`.F6, `zv162_pa" _
        , _
        "rt1$`.F7, `zv162_part1$`.F8, `zv162_part1$`.F9, `zv162_part1$`.F10, `zv162_part1$`.F11, `zv162_part1$`.F12, `zv162_part1$`.F13, `zv162_part1$`.F14, `zv162_part1$`.F15, `zv162_part1$`.F16, `z" _
        , _
        "v162_part1$`.F17, `zv162_part1$`.F18, `zv162_part1$`.F19, `zv162_part1$`.F20" & Chr(13) & "" & Chr(10) & "FROM `zv162_part1$` `zv162_part1$`" & "WHERE `zv162_part1$`.F2 <> Null" _
        )
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_Query_from_Excel_Files13"
        .Refresh BackgroundQuery:=False


    End With

感谢您的回复!

我为查询添加了整个命令

1 个答案:

答案 0 :(得分:1)

试试这个

part1 = "stefl\zv162_part1.xls"

With ActiveSheet.ListObjects.Add( _
   SourceType:=0, _
   Source:=Array(Array("ODBC;DSN=Excel Files;DBQ=" & _
   part1 & _
   ";DefaultDir=cesta;DriverId=1046;FIL=excel 12.0;MaxBufferSize=2048;PageTimeout=5;") _
   ), Destination:=Range("$A$1")).QueryTable