2017-05-29 105 views
-1

下面的代碼來自Excel 2016中從SQL Server Express數據庫導入數據期間記錄的宏。如何在記錄的Excel-VBA代碼中插入可變數據庫名稱?

Sub Macro2() 

    ActiveWorkbook.Queries.Add Name:="Query1", Formula:= _ 
     "let" & Chr(13) & "" & Chr(10) & " Source = Sql.Database(""DESKTOP\SQLEXPRESS"", ""2014-All-1"", [Query=""SELECT *#(lf) FROM [2014-All-1].[dbo].[OtherData]#(lf)#(lf) WHERE volume > 1""])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source" 
    Sheets.Add After:=ActiveSheet 
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _ 
     "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Query1" _ 
     , Destination:=Range("$A$1")).QueryTable 
     .CommandType = xlCmdSql 
     .CommandText = Array("SELECT * FROM [Query1]") 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .PreserveColumnInfo = True 
     .ListObject.DisplayName = "Query1" 
     .Refresh BackgroundQuery:=False 
    End With 
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False 

End Sub 

我試圖讓數據庫名稱的變量,通過增加變量MyDB,這是在代碼的長行使用了兩次,你可以看到。最初這是數據庫的名稱:2014-All-1

不幸的是,這種方法不起作用。有人知道我可以用變量名替換數據庫嗎?

Sub Macro2() 

MyDB = "2014-All-1" 

    ActiveWorkbook.Queries.Add Name:="Query1", Formula:= _ 
     "let" & Chr(13) & "" & Chr(10) & " Source = Sql.Database(""DESKTOP\SQLEXPRESS"", ""MyDB"", [Query=""SELECT *#(lf) FROM [MyDB].[dbo].[OtherData]#(lf)#(lf) WHERE volume > 1 ""])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source" 
    Sheets.Add After:=ActiveSheet 
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _ 
     "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Query1" _ 
     , Destination:=Range("$A$1")).QueryTable 
     .CommandType = xlCmdSql 
     .CommandText = Array("SELECT * FROM [Query1]") 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .PreserveColumnInfo = True 
     .ListObject.DisplayName = "Query1" 
     .Refresh BackgroundQuery:=False 
    End With 
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False 

End Sub 

回答

1

以下是我想接近它:

Sub MyMacro() 

Dim strMyDB as String 
MyDB = "2014-All-1" 

Dim strFormulaTemplate as String 
strFormulaTemplate = "let" & Chr(13) & "" & Chr(10) & " Source = Sql.Database(""DESKTOP\SQLEXPRESS"", ""@@[email protected]@"", [Query=""SELECT *#(lf) FROM [@@[email protected]@].[dbo].[OtherData]#(lf)#(lf) WHERE volume > 1""])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source" 

Dim strFormula as String 
strFormula = replace(strFormulaTemplate,"@@[email protected]@",strMyDB) 

ActiveWorkbook.Queries.Add Name:="Query1", Formula:= strFormula 
... 

End Sub 

如果需要,可以將數據庫名稱作爲參數傳遞,如下所示:

Sub MyMacro(strMyDB as String) 
... 

然後調用宏使用MyMacro("MyDB")

+0

你好史蒂夫,非常感謝這個優雅的結構代碼部分!它工作完美!最好的問候,馬塞爾 – user2165379

+0

不用擔心,很高興有幫助。我討厭所有那些用引號括起來的東西,所以當這種事情出現時,我經常使用'Replace'替代它,而不是試圖使用串聯來構建字符串。我認爲它也容易閱讀。 –

+0

是的,我會在下次有類似的情況時記住這一點。 – user2165379

0

試圖修改 「」 mydb的 「」 來源:CHR(34)& MYDB & CHR(34),如:

"let" & Chr(13) & "" & Chr(10) & " Source = Sql.Database(""DESKTOP\SQLEXPRESS"", " & Chr(34) & MyDB & Chr(34) & ", [Query=""SELECT *#(lf) FROM [" & Chr(34) & MyDB & Chr(34) & "].[dbo].[OtherData]#(lf)#(lf) WHERE volume > 1 ""])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source" 
+0

謝謝,雖然這給出了一個編譯錯誤:預期的命名參數。 – user2165379

+0

謝謝,雖然這給出了編譯錯誤:預期的命名參數。 – user2165379

+0

要匹配原始代碼,請將'FROM [「&Chr(34)&MyDB&Chr(34)&」]'更改爲FROM [「和MyDB&」]'。 (生成的字符串在[括號內]時不需要雙引號。) – YowE3K

相關問題