Select Method of Range class failed - error

scubadiver007

Registered User.
Local time
Today, 07:08
Joined
Nov 30, 2010
Messages
317
Hello,

I have a macro that runs four modules to export four different queries to an Excel spreadsheet, 3 queries to one tab and 1 query to another tab.

They all export ok but I get this error and I am not sure why. Any ideas?
 
Can we see the code you use? I suspect you are using DoCmd.Transferspreadsheet and the last parameter is incorrect.
 
I use the same code for exporting each query (apart from changing the function name and the top left cell) and the first three work fine (including saving and closing the spreadsheet).

The error happens after the fourth set of data is exported and the spreadsheet is saved (but before it is closed).

Option Compare Database

Public Function SendToTable2CUB(strTQName As String, strSheetName As String, strFilePath As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to

' strFilePath is the name and path of the file you want to send this data into.

Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As DAO.Field
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
On Error GoTo err_handler


strPath = strFilePath


Set rst = CurrentDb.OpenRecordset(strTQName)

Set ApXL = CreateObject("Excel.Application")


Set xlWBk = ApXL.Workbooks.Open(strPath)

ApXL.Visible = True

Set xlWSh = xlWBk.Worksheets(strSheetName)

'xlWSh.Range("H5").Select

' For Each fld In rst.Fields
' ApXL.ActiveCell = fld.Name
' ApXL.ActiveCell.Offset(0, 1).Select
' Next

rst.MoveFirst
xlWSh.Range("D14").CopyFromRecordset rst

xlWSh.Range("1:1").select
' This is included to show some of what you can do about formatting. You can comment out or delete
' any of this that you don't want to use in your own export.
With ApXL.Selection.Font
.Name = "Arial"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
End With

'ApXL.Selection.Font.Bold = True

With ApXL.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With

' selects all of the cells
'ApXL.ActiveSheet.Cells.select

' does the "autofit" for all columns
'ApXL.ActiveSheet.Cells.EntireColumn.AutoFit

' selects the first cell to unselect all cells
'xlWSh.Range("E5").select

rst.Close

Set rst = Nothing

ApXL.ActiveWorkbook.Save

ApXL.ActiveWorkbook.Close


Exit_SendTQ2XLWbSheet:
Exit Function

err_handler:

DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Resume Exit_SendTQ2XLWbSheet

End Function
 
Comment out the error handlers, run the code and tell us the exact line where it errors.
 
You should only be using ONE procedure for all of this. You can modify it to accept the starting point as a parameter.

vbaInet said:
Are you using Bob's code?
It is from my site.
 
Sorry to rehash this, but I was hoping I could get some help. Bob, I’m also using your awesome code for exporting data from Access to Excel via macro. I just need resulting data to spit out into a separate tab into 3 templates, so I have a series of runcode actions at the end for each query (ie, SendTQ2XLWbSheet("3a- b KPI query", "KPI Data p2", "U:\Data\KPI Template.xlsx"). I’m getting the 1004 Select Method of Range Class Failed Error once I get to the RunCode actions to export, and only some of the data is successfully exporting.

I’m trying to teach myself VB, which isn’t going so well….so any help would be greatly appreciated! :)
Thanks,
Ellie
 
Sorry to rehash this, but I was hoping I could get some help. Bob, I’m also using your awesome code for exporting data from Access to Excel via macro. I just need resulting data to spit out into a separate tab into 3 templates, so I have a series of runcode actions at the end for each query (ie, SendTQ2XLWbSheet("3a- b KPI query", "KPI Data p2", "U:\Data\KPI Template.xlsx"). I’m getting the 1004 Select Method of Range Class Failed Error once I get to the RunCode actions to export, and only some of the data is successfully exporting.

I’m trying to teach myself VB, which isn’t going so well….so any help would be greatly appreciated! :)
Thanks,
Ellie
Start a new thread on this and be sure to include all of the code you are trying to use with this (using code tags on the forum here so it is easier to read) and then if I can, I'll look but if not, I'm sure someone will be able to help.


codetag001.png
 

Users who are viewing this thread

Back
Top Bottom