VBA CopyFromRecordset Range in Excel with Examples

CopyFromRecordset of Excel VBA Range method can be used if you want to copy the data from a record set. When we deal with data bases using ADO or DAO, we often retrieve the data using select statement to a record set, and we process to an array or we paste into a range. CopyFromRecordset method of Excel Range object will help to copy the data from record set to a specific range in the Excel using VBA.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


VBA CopyFromRecordset Method Excel Range Object

VBA CopyFromRecordset Range in Excel- Syntax

Here is the syntax of the CopyFromRecordset method of range object.

Range(“YourRange”).CopyFromRecordset(Data As Unknown,

[MaxRows], [MaxColumns])

Here,
Data is the record set (rs)
MaxRows are the maximum records to be copied, by default all records will be copied.
MaxColumns are the maximum fields to be copied, by default all fields will be copied.

VBA CopyFromRecordset Range in Excel- Examples

Here is the simple example to use VBA to Copy the record set into Excel Worksheet.

Sub CopyFromRecordset_To_Range()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
‘DBPath = "C:\InputData.xlsx"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = "SELECT * From [Sheet1$]"
rs.Open sSQLSting, Conn
'=>Load the Data into an array
‘ReturnArray = rs.GetRows
”OR”
'=>Paste the data into a sheet
Sheet2.Range("A2").CopyFromRecordset rs
'Close Recordset
rs.Close
'Close Connection
Conn.Close
End Sub

Here is the full explanation and working example file to download:

Working Example on VBA Range CopyFromRecordset

VBA to Copy the 10 records from the Record Set – Example

Here is the example syntax to copy only the 10 records of the record set:

Sheet2.Range("A2").CopyFromRecordset rs,10

VBA to Copy the 5 fields from the Record Set – Example

Here is the example syntax to copy only the 10 records of the record set:

Sheet2.Range("A2").CopyFromRecordset rs,,5

CopyFromRecordset method will take some time to copy the data into worksheet. The last two example are generally used while testing a connection to a data base. So that we can save some time .

LIMITED TIME OFFER

3 Comments

  1. Max February 26, 2016 at 10:03 PM - Reply

    How do I get the 2nd and the 5th field from the record set? I have the first I have field names already in the spreadsheet, and don’t want to include that and fields 3 and 4.

  2. kapil August 22, 2016 at 12:29 AM - Reply

    how do we get the data from ms access table and how we connect to it?
    please help

  3. prabhat January 23, 2017 at 8:12 PM - Reply

    i have used exactly same technique to connect to db(sql server) but the mentioned statement (Sheet2.Range(“A2”).CopyFromRecordset rs) is not copying any record to the sheet. it is not throwing any error as well. Please help.

Leave A Comment


Related pages


microsoft excel vba programming for the absolute beginneradvanced excel vba tutorialif statement excel examplespassword protect excel 2010 spreadsheetexcel vba function rangevba tutorials pdfoutlook.application vbawhat is the formula for adding a column in excelaccess vba close tablevba macro excelvba rgb color codesexcel functions lookupshortcut key for replace in excelresidential construction schedule template excelunhide on excelexcel hiding columnsmeeting agenda word templatedml and ddlif iserror formulaunderstanding excel formulas and functionssoftware development estimation spreadsheetvba accessvba to close workbookhow to understand excel formulasvba if iserrorexcel 2010 vba select casevba destinationlearn macros in excel 2007 pdfhloookupvlookup yes novba code examples for excelrank vbahow to protect spreadsheet with passwordvba forecolor codespivot table templatesagenda excel templatevba countexcel auto row heightmerge tabs excelvba create hyperlinkexcel sort column alphabeticallyhow to use if condition in excel 2007excel to xml conversionexcel transpose macroexcel vba write to text filevba read text filevlookup definition wikipediadeclaring arrays in vbaexcel vba close workbookremove duplicate data in excelcountif tutorialunlock excel file password protectedvba language referenceexcel vba merge cellsautofit in excel vbanested if and statements in excel 2010excel vba exitvba savedelete duplicate rows in excelhlookup examplesmacro excel definitioncost analysis template excelexcel hide cellshow to use excel match functionbubble graphs in excelproject management gantt chart excel templateexcel multiple if statements in one cellhow to run macros in excelhow to remove blank rows excelhow to add command button in excel 2007excel with vba programmingawesome excel macrosprotect workbook excelinstr vba functionvba rtrimhow to use average function in excel 2007if statements in excel vbaloops in excel