2016-12-12 2 views
1

私は現在、ExcelマクロからSQLストアドプロシージャを実行しています。返されたレコードの数が1シートの最大行を超えています。オーバーフローの結果を2番目のシートに転送するにはどうすればよいですか?VBA - シート1がいっぱいの場合、シート2の結果を返す

Sub Button1_Click() 
Dim con As ADODB.Connection 
Dim cmd As ADODB.Command 
Dim rs As ADODB.Recordset 
Dim par As String 
Dim WSP1 As Worksheet 
Set con = New ADODB.Connection 
Set cmd = New ADODB.Command 
Set rs = New ADODB.Recordset 

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

' Remove any values in the cells where we 
' want to put our Stored Procedure's results. 
Dim rngRange As Range 
Set rngRange = Range(Cells(8, 2), Cells(Rows.Count, 1)).EntireRow 
rngRange.ClearContents 


' Log into our SQL Server, and run the Stored Procedure 
con.Open "Provider=SQLOLEDB;Data Source=67.09;Initial Catalog=TEST..." 
cmd.ActiveConnection = con 

Application.StatusBar = "Running stored procedure..." 
cmd.CommandText = "SP_Billing" 
Set rs = cmd.Execute(, , adCmdStoredProc) 

' Copy the results to cell B7 on the first Worksheet 
Set WSP1 = Worksheets(1) 
WSP1.Activate 
If rs.EOF = False Then WSP1.Cells(8, 2).CopyFromRecordset rs 

rs.Close 
Set rs = Nothing 
Set cmd = Nothing 

con.Close 
Set con = Nothing 

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

objRecordset.MoveNextを使用してみてください、あなたはないRS.EOF do-while文をwithing全てに書き込みをしているセルを追跡 – ivan7707

答えて

0

あなたのレコードセットを解析中にあなたが(例えば、あなたが印刷された後のページを切り替えるなど、100kの行を言う)、いくつかのカスタム処理が必要な場合は、もはやRange.CopyFromRecordsetメソッドを使用することはできません。代わりに、レコードセットを繰り返し処理する必要があります。ここではもちろん、離れて全体のパズルを与えることなく(そのようなことを行う方法の小さなサンプルです:

Dim i_RowCount As Long 
Dim a_PrintArray As Variant, rg_PrintRg As Range 
Dim i_Col As Integer 
Const i_MaxRows As Long = 100000 
' I recommend filling everything into an Array first and then Printing the array to Excel' 

' Using your existing variables also ' 
ReDim a_PrintArray(1 to i_MaxRows, 1 to rs.Fields.Count) 
Set sh_Current = WSP1 
Do Until rs.EOF 
    i_RowCount = i_RowCount + 1 

    If i_RowCount > i_MaxRows Then 'If we hit the max, print what we have' 
     ' Setting up the print range to match the array size ' 
     Set rg_PrintRg = shCurrent.Cells(8, 2) 
     Set rg_PrintRg = Range(rg_PrintRg, rg_PrintRg.Offset(i_MaxRows - 1, rs.Fields.Count - 1)) 
     rg_PrintRg = a_PrintArray ' Print the array into the range ' 
     i_RowCount = 1 
     Set sh_Current = sh_Current.Next 
     ReDim a_PrintArray(1 to i_MaxRows, 1 to rs.Fields.Count) 
    End If 

    For i_Col = 0 To rs.Fields.Count - 1   
     a_PrintArray(i_RowCount, i_Col) = rs.Fields(i_Col).Value 
    Next i_Col 

    rs.MoveNext 
Loop 

このコードsnippitが唯一のデモのためであることに注意してくださいそれがコンパイルされておらず、最適ではないかもしれません。特定のアプリケーションのためのレコードセットオブジェクトの詳細については:あなたはEOFを打つまでhttps://msdn.microsoft.com/en-us/library/ms681510%28v=vs.85%29.aspx

1

ちょうど.CopyFromRecordsetとループにMaxRowsパラメータを渡す各呼び出しは、レコードにカーソルを進め、コピーが現在のカーソル位置から開始します。私はそれをSubのようなものに解凍します...

Private Sub SplitRecordsToSheets(records As ADODB.Recordset, perSheet As Long) 
    Dim ws As Worksheet 
    Do While Not records.EOF 
     Set ws = Worksheets.Add 
     ws.Cells(8, 2).CopyFromRecordset records, perSheet 
    Loop 
End Sub 

...そして、このようにそれを呼び出す:

' Log into our SQL Server, and run the Stored Procedure 
con.Open "Provider=SQLOLEDB;Data Source=67.09;Initial Catalog=TEST..." 
cmd.ActiveConnection = con 

Application.StatusBar = "Running stored procedure..." 
cmd.CommandText = "SP_Billing" 
Set rs = cmd.Execute(, , adCmdStoredProc) 

SplitRecordsToSheets rs, ActiveSheet.Rows.Count - 8 
関連する問題