export to excell

reza_doang

Board Regular
Joined
May 31, 2010
Messages
187
dear expert...

i have 1 file access and 1 table in that file.
In that table i have data for about 5 million row...
how to convert/export to excel?

if i use usual way, i can only export for 65.000 row.

thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try creating a number of queries then use a macro to transfer the queries to a single excel file, it will build its own worksheets based on the queries, so all your data will go a workbook but on different tabs.
 
Upvote 0
I don't know your data but create a query and set some type of parameter like show 1 to 66530 (ID Field) save the query then do the next one 66531 to next number and save then carry on until you have all the records going into different queries.

Next select your macros section or add create new macro (depending on which version of Access you are using), then select the action to transferSpreadSheet in the arguments below select

Transfer Type = Export
Table Name = your first query name goes here
File Name = C:\Export.xls (Or something like this)
Has Field Names = Yes

Then repeat this with another one of the queries as the next action and test it to see that it exports Ok, once your happy just add the rest in the macro.
 
Upvote 0
The problem is that Excel can only hold 65.000 records in one sheet, and 1.048.576 if you are using Excel 2007 or higher.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
At first you should consider if you really want 5 million records in an Excel workbook, but if you have a good reason for this, read on.<o:p></o:p>
<o:p> </o:p>
What you could do is split the data from the Access table in lets say 100 sheets, each one containing 50 thousand rows. Of course if you are on Excel 2007 or up, you’ll only need 5 sheets of 1 million rows.<o:p></o:p>
<o:p> </o:p>
The code to do this is: (you can copy this to a module in Access, mind the references needed)
Code:
Option Compare Database
Option Explicit
Public Function SQL_TableX() As String
[COLOR=seagreen]'Query string to retrieve al fields and records from TableX
'TableX should be substituted with you table name
[/COLOR]SQL_TableX = "Select * from TableX"

End Function
[COLOR=seagreen]'Set reference to Microsoft Excel xx Object Library
'Set reference to Microsoft ActiveX Data Objects 2.x Library
'Free to use, coded by MIO-Software Netherlands
[/COLOR]Public Sub CreateExcel()

Dim oCn As New ADODB.Connection
Dim oRs As New ADODB.Recordset
Dim oExcelApp As New Excel.Application
Dim oExcelWB As New Excel.Workbook
Dim oExcelSht As Excel.Worksheet
Dim sWorkbookName As String
Dim sSheetName As String

Dim arrAllRecords() As Variant
Dim arrSubRecords() As Variant
Dim lRecNo As Long
Dim lRecNoSub As Long
Dim lMaxRecPerSheet As Long
Dim lRunningTot As Long
Dim iNumbSheets As Integer
Dim iSheetCnt As Integer
Dim iFldCnt As Integer
Dim i As Integer
Set oCn = CurrentProject.Connection
[COLOR=seagreen]'Here type the maximum rows you want per sheet
[/COLOR][COLOR=sienna][B]lMaxRecPerSheet = 1000[/B][/COLOR]
With oRs
    
    .Open SQL_TableX, oCn, adOpenStatic, adLockReadOnly
        arrAllRecords = oRs.GetRows()
        iFldCnt = .Fields.Count - 1
    .Close
End With
Set oRs = Nothing
'Calculate number of sheets needed
    iNumbSheets = Round((UBound(arrAllRecords, 2) / lMaxRecPerSheet) + 0.5)
[COLOR=seagreen]    'you can skip this, it's just a security to avoid overstack, but better don't skip
[/COLOR]    If iNumbSheets > 255 Then
        MsgBox "Can't create more then 255 sheets", vbExclamation
        Exit Sub
    End If
    
'create Excel object
[COLOR=seagreen]'Here comes your path and workbookname
[/COLOR][B][COLOR=sienna]sWorkbookName = "M:\CreateExcel\MyExcelBook"[/COLOR][/B]
With oExcelApp
         
        lRunningTot = 0
        iSheetCnt = 1
            'Create a new workbook
            Set oExcelWB = Workbooks.Add
            With oExcelWB
            
                 For lRecNo = lRunningTot To UBound(arrAllRecords, 2)
                        [COLOR=seagreen]'If last sheet, dimension arrSubRecords to the number of left records
[/COLOR]                        If iSheetCnt = iNumbSheets Then
                            ReDim arrSubRecords(iFldCnt, UBound(arrAllRecords, 2) - lRunningTot)
                        Else
                            ReDim arrSubRecords(iFldCnt, lMaxRecPerSheet - 1)
                        End If
                        
                       [COLOR=seagreen] 'Fill array subrecords
[/COLOR]                        For lRecNoSub = 0 To UBound(arrSubRecords, 2)
                            For i = 0 To iFldCnt
                                    arrSubRecords(i, lRecNoSub) = arrAllRecords(i, lRecNoSub + lRunningTot)
                            Next i
                        Next lRecNoSub
                        
                       [COLOR=seagreen] 'When subrecords complete, create a workhsheet for the records
[/COLOR]                        sSheetName = "Sheet " & iSheetCnt & " of " & iNumbSheets
                        
                        Set oExcelSht = .Worksheets().Add
                        'Copy array to new sheet
                        With oExcelSht
                                .Name = sSheetName
                                .Cells(1, 1).Resize(UBound(arrSubRecords, 2) + 1, UBound(arrSubRecords, 1) + 1).Value = _
                                 oExcelApp.WorksheetFunction.Transpose(arrSubRecords)
                        End With
                        
                        iSheetCnt = iSheetCnt + 1
                        lRunningTot = lRecNoSub + lRunningTot
            
                        lRecNo = lRunningTot - 1
               
[COLOR=seagreen]                  'Move one to next chunck
[/COLOR]                  Next lRecNo
        End With
    oExcelWB.SaveAs sWorkbookName
[COLOR=seagreen]    'Destroy object
[/COLOR]    .Quit
    Erase arrSubRecords
    Erase arrAllRecords
End With
    
    
End Sub
 
Upvote 0
Just out of curiosity, why do you need to export 5 million rows to Excel?
What are you doing with this data when it is in Excel?
I can't imagine whatever it is going to run that efficiently with that many rows on multiple sheets in Excel.

Just wondering if there is a better way to accomplish whatever it is that you are trying to do.
 
Upvote 0
All..

thanks for your response.
Here the situation: i got data from data entry, but they give me a mdb file which is contain about 5 million row.
from that data i want to split based on criteria, let say based on city.
If i should use simple/usual way, it take much time so i need vba to simple this.

i'm using office 2010.


thanks

reza
 
Last edited:
Upvote 0
Kreszch68...

i tried your vba, but there some error...
"Not enough storage is available to complete this operation"

when i press bug, go to "arrAllRecords = oRs.GetRows()"

any idea...thanks
 
Upvote 0
Hi Reza,

Maybe you could try to put a where clause in your query to limit the amount of records pulled into the array.
In the sample code I've placed a very simple query that retrieves all fields and all records from your table.
However, after reading your second last post, I'm wondering if pulling all data to Excel is the best way to go. Excel is not the tool to do what you're after, Access is designed to do the job.
 
Upvote 0
All..

thanks for your response.
Here the situation: i got data from data entry, but they give me a mdb file which is contain about 5 million row.
from that data i want to split based on criteria, let say based on city.
If i should use simple/usual way, it take much time so i need vba to simple this.

i'm using office 2010.


thanks

reza

I would be very dubious about putting 5 million rows (how many fields, by the way?) into Excel. If that goes into one file you may not be able to open it.
Is there a good reason to put it into Excel instead of analysing it in Access?

Denis
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

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