2017-08-16 4 views
0

SQL ServerからExcelにデータをプルするクエリが2つあります。どちらも完璧に動作します。私は一緒に結合し、日付としてパラメータを渡そうとしましたが、今は何も動作しません。ここでは、Microsoft Queryを使用してパラメータを渡す方法を説明する記事へのリンクを示します。Excelのパラメータ外部データユニオンクエリ

http://dailydoseofexcel.com/archives/2004/12/13/parameters-in-excel-external-data-queries/

私がやりたいことのすべては、ユニオンクエリで、この作業を取得します。それは可能ですか?または、これを実現するためにVBAソリューションが必要ですか?私はそれができると確信しています、私はちょうどそれを行う方法を正確に知らない。私は何か提案を感謝します。

ありがとうございます!

+1

質問を投稿する –

答えて

0

ありがとうございました。私はVBAでやりました。

Sub ImportFromDB() 

' Create a connection object. 
Dim cnPubs As ADODB.Connection 
Set cnPubs = New ADODB.Connection 

' Provide the connection string. 
Dim strConn As String 

'Use the SQL Server OLE DB Provider. 
strConn = "PROVIDER=SQLOLEDB;" 

'Connect to the Pubs database on the local server. 
strConn = strConn & "DATA SOURCE=SERVER_NAME;INITIAL CATALOG=Data_Base;" 

'Use an integrated login. 
strConn = strConn & "Trusted_Connection=Yes;" 

'Now open the connection. 
cnPubs.Open strConn 

' Create a recordset object. 
Dim rsPubs As ADODB.Recordset 
Set rsPubs = New ADODB.Recordset 
Set sht = Worksheets("Impact Analysis") 

    With sht 
     .Range("N:U").ClearContents 
     .Range("N1").Resize(1, 8).Value = Array("CONTACT_ID", "CATEGORY", "COMPANY_CODE", "CUSTOMER_NO", "SECTOR", "DES", "ASOFDATE", "BALANCE") 
     Set rw = .Rows(2) 
    End With 

    With rsPubs 
     ' Assign the Connection object. 
     .ActiveConnection = cnPubs 
     ' Extract the required records. 
     .Open "SELECT * FROM MY_TABLE" 

     ' Copy the records into cell A1 on Sheet1. 
     Worksheets("Impact Analysis").Range("N2").CopyFromRecordset rsPubs 

     ' Tidy up 
     .Close 
    End With 

cnPubs.Close 
Set rsPubs = Nothing 
Set cnPubs = Nothing 

End Sub 
関連する問題