I'm writing some vb to export data from access to excel. Once passed to excel, I use code to format one spreadsheet in the workbook, then save and close the file. It is supposed to loop through this code several times. When I execute the code, it creates the first itteration of the spreadsheet, but on the second pass it returns a 1004: application defined or object defined error. Been searching for a solution and cannot find. Help please?
===============================================
Code is below
On Error GoTo errorhandler
Dim strtable As String
Dim strworksheetpath As String
Dim pickersct As Integer
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSh As Excel.Worksheet
pickersct = 1
Me.Text7 = 1
Do Until pickersct > Me.Text1
Me.Text7.Requery
strworksheetpath = "D:\My Documents\Deliverable" & pickersct & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qry_pickers_Picks_Export", strworksheetpath
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("D:\My Documents\deliverable" & pickersct & ".xls")
Set xlSh = xlWB.Sheets("qry_pickers_Picks_Export")
Sheets("qry_pickers_Picks_Export").Activate 'Error occurs after this line
Sheets("qry_pickers_Picks_Export").Range("H2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=1,-1,0)"
Sheets("qry_pickers_Picks_Export").Range("I2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=1,-1,0)"
Sheets("qry_pickers_Picks_Export").Range("J2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]+RC[-1]"
Sheets("qry_pickers_Picks_Export").Range("J2").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
Formula1:="=1"
xlWB.Save
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
pickersct = pickersct + 1
Me.Text7 = Me.Text7 + 1
Loop
errorHandlerExit:
Exit Sub
errorhandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume errorHandlerExit
xlSh.Range("H2").FormulaR1C1 = "=IF(RC[-3]=1,-1,0)"plus you define xlSh but then don't use it, as in the code in my comment I included this. - DragonSamu