exporting from record and saving to separate xls file in folder.

bgrove

Board Regular
Joined
Dec 2, 2013
Messages
60
Hi,

I'm having an issue with my LOOPING AND SAVING CODE for MSACCESS.

I have a file that has 1.5 million rows. I need to pull 4000 at a time then export it to a workbok and save that work book with the row number range in the file name. example

Test_1_4000.xls
Test_4001_8000.xls

Here is my code. I shortened the number of rows needed in the code for testing purposes.

The problem is that when I run the code All 20 rows export not just 5 and my save file gets and error.

I would LOVE someones help on this. I don't want to export manually.

Option Explicit
Sub Export2Excel()


Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fileName As Object
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim x1APP As Excel.Application
Dim objWkb As Workbook
Dim objSht As Worksheet
Dim i As Integer
Dim j As Integer


'1)Identify the database and query
Set db = CurrentDb
Set rs = db.OpenRecordset("Pinterest_Query", dbOpenDynaset)

'2)Clear previous contents
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.Workbooks.Add
.Sheets("Sheet1").Select


'3)Add column headings
For i = 1 To rs.Fields.Count
xlApp.ActiveSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next i
xlApp.Cells.EntireColumn.AutoFit

'4)count number of rows and copy to worksheet

For i = 1 To Int(rs.RecordCount / 5) + 1
For j = 1 To 5
If Not rs.EOF Then
ActiveSheet.Range("a2").CopyFromRecordset rs
rs.MoveNext
End If
Next
Next

End With
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,221,831
Messages
6,162,242
Members
451,756
Latest member
tommyw

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