Import into Excel from Access table

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I work on Excel reports.

I need to import data from a source that has 2 million rows or more.
Because of Excel row constraint, I did an import into an Access DB table.

I need to download this data to excel by splitting the data between two worksheets since Excel has row constraints.
How do I import using Excel VBA by specifying the row number (until 1,048,576 rows into WS1 and greater than that into WS2)?

Or is there any alternate way?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
'ADD VBA REFERENCE: Microsoft ActiveX Data Objects 6.x Library
( alt-F11 (vbe), tools , references)

try this. Make 2 queries:
1. (qsMyDataQuery1 ) to pull TOP 1.x million records.
2. qsMyDataQuery2, to pull the rest.

then the code will grab the 1st query, paste to sheet1
then grabs the next query, and pastes to sheet2


Code:
Public Sub CopyData()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim DB
Dim vProvid
Dim i As Integer

Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
 
DB = "\\server\folder1\myDatabase.mdb"
vProvid = "Microsoft.Jet.OLEDB.4.0"        ' or for Sqlsvr:  "SQLOLEDB"
 
 
With con
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Open "Data Source=" & DB & ";Jet OLEDB"
End With
 
On Error Resume Next
For i = 1 To 2
    AddSheet i
    Set rs = con.Execute("qsMyDataQuery" & i)
    If rs.RecordCount > 0 Then
       ActiveWorkbook.Worksheets("Sheet" & i).Range("A1").CopyFromRecordset rs
    End If
Next
rs.Close
con.Close
End Sub

private Sub AddSheet(ByVal pvShtNo)
Dim vSht
 On Error GoTo MakeSht
 
vSht = "sheet" & pvShtNo
 
If Worksheets(vSht).Name = vSht Then
End If
Exit Sub

MakeSht:
 Sheets.Add
 ActiveSheet.Name = vSht
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top