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

Access 2000 Timeouts on bound recordset
Query to a Meaningful Report
MS Access “duplicate values” error, but I don't know why
Generate uml from vba
Is it possible to change Msysobjects in ms access
How can I make MS-Access choose a different/the right execution plan for my query
Access 2007 Reports - How to 'unfix' fields?
Hide Access Options
MS Access “record changes” log
Format combox in Access 2007
MS Access how can a subform get the value from another subform?
Can you use ADO to edit table schema in MS Access?
Access 2010: 'the expression cannot be used in a calculated column'?
ACCESS SubForm: trying to add records
Checking social security numbers and business identity codes in Access 97
MS Access VBA Substitution Cipher Encrypt/Decrypt

Categories

HOME
hive
atom-editor
hook
pypi
tesseract
rubygems
rdf
sqlite3
survey
in-app-purchase
portia
multiple-records
pheatmap
slick-slider
cloudkit
kentor-authservices
handsontable
dtrace
fallback
clojurescript
grails3
visjs
autoconf
wkwebview
shopware
pepper
user-interaction
ping
windowbuilder
google-cloud-nl
frame
binary-data
preg-match-all
titanium-mobile
gammu
mpmediaquery
jmonkeyengine
wixsharp
x11-forwarding
serverside-rendering
dism
particles.js
core-plot
firebase-admin
eigenvalue
estimote
modelmapper
gpx
reactive-cocoa-5
segment
google-datalayer
qsslsocket
prolog-setof
winscp-net
fancybox-2
magma
lift-json
capacity
angular-strap
medium.com
xml-attribute
impresspages
storekit
yaws
apachebench
itextpdf
qtableview
jms2
python-3.2
method-parameters
unity3d-gui
census
pretty-print
streambase
rake-task
ng-animate
nsbutton
wordpress-theme-customize
neolane
document-classification
onactivityresult
project-planning
sgen
pyhdf
geos
quantlib-swig
spring-io
gridcontrol
random-seed
sqlperformance
flashvars
selected
hamiltonian-cycle
dsn
dbproviderfactories
genshi
tomcat-valve
invite
ecl
winbugs14
mysql-error-1005
castle-monorail
rescale
audio-capture
sef
memory-size
photoshop-cs4
ctp4
text-coloring
zend-decorators

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