2017-06-13 4 views
2

スプレッドシートを使用してSQL Serverストアドプロシージャを呼び出し、データを返します。 Excel 2010で100%動作し、何ヶ月もしました。ただし、Excel 2013の一部のユーザーにとっては、すべてのデータが返されるわけではありません.0を使用するだけです。ここに私のコードです:Excel 2010とExcel 2013 VBAストアドプロシージャ

Sub Button10_Click() 
    Set con = New ADODB.Connection 
    Set cmd = New ADODB.Command 
    Set rs = New ADODB.RecordSet 

    Sheets("Customer").Visible = True 

    'Unprotect worksheet 
    Worksheets("Customer").Unprotect Password:="*******" 

    ' remove and re-add auto filter 
    Sheets("Customer").AutoFilterMode = False 
    Sheets("Customer").Range("A4:AO4").AutoFilter 

    Application.DisplayStatusBar = True 
    Application.StatusBar = "Contacting SQL Server..." 

    ' Remove any values in the cells where we want to put our Stored Procedure's results. 
    With Sheets("Customer") 
     .Rows(5 & ":" & .Rows.Count).Delete 
    End With 

    ' Log into our SQL Server, and run the Stored Procedure 
    con.Open "Provider=SQLOLEDB;Data Source=*********;Initial Catalog=**********;Integrated Security=SSPI;Trusted_Connection=Yes;" 
    cmd.ActiveConnection = con 

    Application.StatusBar = "Running stored procedure..." 

    '===== Check if the user has ticked the 'Include Current Month Totals checkbox ==== 
    If Range("$M$1") = True Then 
     'include current month in totals 
     cmd.CommandText = "usp_Customer_Lvl1_Current_Month_INC"  
    Else 
     'do not include current month in totals 
     cmd.CommandText = "usp_Customer_Lvl1_Previous_Month_INC" 
    End If 

    Set rs = cmd.Execute(, , adCmdStoredProc) 

    '========== 
    Sheets("Customer").Range("A5").CopyFromRecordset rs 
    Application.Goto Reference:=Worksheets("Customer").Range("A5") 

    'protect worksheet 
    Worksheets("Customer").Protect Password:="***********", AllowFormattingCells:=True, AllowFiltering:=True 

    '============= 

    rs.Close 
    Set rs = Nothing 
    Set cmd = Nothing 

    con.Close 
    Set con = Nothing 

    Application.StatusBar = "Data successfully updated." 
End Sub 

これはVBAのものでなければなりません。データはSQLとExcel 2010にあります - どのようなアイデアですか?

+0

エラーメッセージはありますか? –

+0

いいえ - それはすべてのデータを返しません - 非常に奇妙な – Michael

+0

私の[過去の回答](https://stackoverflow.com/questions/28677262/recordset-closed-after-stored-procedure-execution)を確認してください。あなたはSPに関する非常に有益な情報を見つけることができます。 –

答えて

0

私はそれを試しました。私はテキストボックスから値を取る。エクセル2010年に私は以下を使用します。

場合、範囲( "$ M $ 1")= trueの場合、

ただし、Excel 2013で、あなたも、同様のワークシートを参照する必要が表示されます。

Sheets( "Home")。Range( "$ M $ 1")= True Then

とにかく、シート名を含むとにかく意味するようになりました。