ms-access


Export data from Access 2010 to Excel 2013


i am exporting data from Access to Excel using recordset to transfer data from access query to excel(as I have to manual formatting which cannot be done with transferSpreadsheet) and while i am using the code
with sheet1
.range("A2").CopyRecordset rs1
End With
This works fine till 3 sheets but when I initiate 4th sheet (as Excel has 3 sheets by default)
Set sheet4 = wb.Worksheets.Add
I am getting an error saying
Subscript out of range error.
Can someone help me out with the same?
Which line errors - adding the worksheet?
Code works for me:
Set Sheet4 = Sheets.Add
Maybe post your full procedure for analysis.
Without seeing the code it's impossible to say for sure. Maybe a worksheet name is misspelled. Just a guess. Try the code samples below for some different ways of how to do this kind of task.
'************* Code Start *****************
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Sub sCopyFromRS()
'Send records to the first
'sheet in a new workbook
'
Dim rs As Recordset
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim objXL As Excel.Application
Dim objWkb As Workbook
Dim objSht As Worksheet
Set rs = CurrentDb.OpenRecordset("Customers", _
dbOpenSnapshot)
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast: rs.MoveFirst
intMaxRow = rs.RecordCount
Set objXL = New Excel.Application
With objXL
.Visible = True
Set objWkb = .Workbooks.Add
Set objSht = objWkb.Worksheets(1)
With objSht
.Range(.Cells(1, 1), .Cells(intMaxRow, _
intMaxCol)).CopyFromRecordset rs
End With
End With
End If
End Sub
Sub sCopyRSExample()
'Copy records to first 20000 rows
'in an existing Excel Workbook and worksheet
'
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Dim rs As Recordset
Dim intLastCol As Integer
Const conMAX_ROWS = 20000
Const conSHT_NAME = "SomeSheet"
Const conWKB_NAME = "J:\temp\book1.xls"
Set db = CurrentDb
Set objXL = New Excel.Application
Set rs = db.OpenRecordset("Customers", dbOpenSnapshot)
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Worksheets(conSHT_NAME)
If Not Err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
intLastCol = objSht.UsedRange.Columns.Count
With objSht
.Range(.Cells(1, 1), .Cells(conMAX_ROWS, _
intLastCol)).ClearContents
.Range(.Cells(1, 1), _
.Cells(1, rs.Fields.Count)).Font.Bold = True
.Range("A2").CopyFromRecordset rs
End With
End With
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set db = Nothing
End Sub
Sub sCopyRSToNamedRange()
'Copy records to a named range
'on an existing worksheet on a
'workbook
'
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Dim rs As Recordset
Const conMAX_ROWS = 20000
Const conSHT_NAME = "SomeSheet"
Const conWKB_NAME = "c:\temp\book1.xls"
Const conRANGE = "RangeForRS"
Set db = CurrentDb
Set objXL = New Excel.Application
Set rs = db.OpenRecordset("Customers", dbOpenSnapshot)
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Worksheets(conSHT_NAME)
If Not Err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
objSht.Range(conRANGE).CopyFromRecordset rs
End With
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set db = Nothing
End Sub
'************* Code End *****************

Related Links

Microsoft Access Query through VBA gets repeated?
What sort of functionality can I expect from exporting a Microsoft Access 2007 Database to a WSS 3.0 site?
query muliple fields with multiple values
Force OpenCurrentDatabase to use Access 2003
ACCESS/VBA: How to create a ON/OFF type switch to allow record modification on a form?
dsn for ms access 2003 in windows 7
How can I find all the MS Access security groups in VBA
Dropdownmenu's linked?
Drop all connection to MS Access Database using ADO.NET
Import MS Access to CSV on Mac programmatically
MS Access - Is there a limit to Public Functions/Routines in MS Access? RTE #7?
Need to Allow Null or “” values for Dates - Access Query Data type mismatch
How to migrate MS access database to Oracle?
How to make a delete query in design view
Reserved error (-1524)
MS Access Import Error Table

Categories

HOME
deployment
amazon-ec2
activiti
ngrx
kde
office365api
vmware
grep
mean-stack
v8
yum
slurm
tomcat6
windows-azure-storage
windows-server-2012
serverless-framework
abi
functional-testing
tar
mmap
tibco-mdm
crosstab
buildbot
cas
chromebook
web-api-testing
dbclient
windowbuilder
srcset
catch-all
accelerate-framework
bower-install
bytecode-manipulation
vxworks
retina-display
neuroscience
unspecified
webix-treetable
sqlite2
opshub
elasticsearch-plugin
awt
eclipse-scout
ncalc
grid.mvc
core-plot
arena-simulation
import-from-excel
checkboxlist
reactive-cocoa-5
angular-resource
google-datalayer
multipeer-connectivity
paxos
boost-multi-index
blogengine.net
sage-one
idisposable
setter
pearson
memory-alignment
arrow-keys
migradoc
jquery-filter
nsfilemanager
autorest
bstr
angstrom-linux
disque
react-native-listview
system.management
reactive-banana
cyclomatic-complexity
qdialog
wordml
cakephp-3.1
sankey-diagram
sonarqube5.1.2
skos
c++03
gwidgets
ceil
valueconverter
marmalade
mysql-error-1062
concurrent-collections
centos5
pyhdf
mysqltuner
chronoforms
jquery-mobile-dialog
kyotocabinet
angularjs-controller
simba
unc
ocx
spring-portlet-mvc
netdna-api
joyent
floating
lpeg
radcombobox
coda-slider
xsdobjectgen
bespin
w3wp
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App