2017-03-01 68 views
1

我有excel文件(VBA),我希望在單獨的文件中輸出,但我努力使其追加。 輸出應該是工作表中第6行以下的所有內容。將VBA中的輸出從Activesheet更改爲新文檔(文件)

Set RST = New ADODB.Recordset 
RST.Open SQL, mCON, adOpenForwardOnly, adLockReadOnly, adCmdText 

Do Until RST.EOF = True 
    Application.StatusBar = "Getting data to row: " & lRow 

    For n = 0 To (RST.Fields.Count - 1) 
     If Not IsNull(RST(n)) Then ActiveSheet.Cells(lRow, n + 1) = RST(n) 

    Next 

    RST.MoveNext 
    lRow = lRow + 1 
Loop 

ActiveSheet.Range("B5").Select 

Application.ScreenUpdating = True 

Exit Sub 

回答

1

只需創建一個新的工作簿:

Dim wB As Excel.Workbook 

Set RST = New ADODB.Recordset 
RST.Open Sql, mCON, adOpenForwardOnly, adLockReadOnly, adCmdText 
Set wB = Workbooks.Add 
lRow = 6 

Do Until RST.EOF = True 
    Application.StatusBar = "Getting data to row: " & lRow 

    For n = 0 To (RST.Fields.Count - 1) 
     If Not IsNull(RST(n)) Then wB.Sheets(1).Cells(lRow, n + 1) = RST(n) 
    Next n 
    RST.MoveNext 
    lRow = lRow + 1 
Loop 

wB.Sheets(1).Range("B5").Select 

Application.ScreenUpdating = True 
Exit Sub 
相關問題