Hi, i have some coding which exports a query into Excel and then a graph is manipulated from this. the coding works, but the second time you run it I get a runtime 1004 'global error'
Below is my code, can anyone tell me what the problem is:
Dim myExcel As New Excel.Application
Dim myBook As Excel.Workbook
Dim mySheet As Excel.Worksheet
Dim rs As Recordset
Dim db As Database
Dim sSQL As String
Dim i As Integer
myExcel.Visible = True
Set myBook = myExcel.Workbooks.Add(xlWBATWorksheet)
'SA total
Set mySheet = myBook.ActiveSheet
'MsgBox mySheet.Name
mySheet.Name = "Austria"
Set db = CurrentDb
sSQL = db.QueryDefs("Booking_Curve_Austria").SQL
Set rs = db.OpenRecordset(sSQL)
For i = 1 To rs.Fields.Count
mySheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next i
mySheet.Cells(2, 1).CopyFromRecordset rs
'create graph
Range("A1:A8").Select
Range(Selection, Selection.End(xlToRight)).Select
Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("Austria").Range("A1:AB8"), PlotBy _
:=xlRows
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="BookingCurve"
With ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
'Save sheet
mySheet.SaveAs "P:\Documents\Austria_Booking_curve.xls"
'Close excel file
myBook.Close False
myExcel.Quit
Thanks
Dharmesh
Below is my code, can anyone tell me what the problem is:
Dim myExcel As New Excel.Application
Dim myBook As Excel.Workbook
Dim mySheet As Excel.Worksheet
Dim rs As Recordset
Dim db As Database
Dim sSQL As String
Dim i As Integer
myExcel.Visible = True
Set myBook = myExcel.Workbooks.Add(xlWBATWorksheet)
'SA total
Set mySheet = myBook.ActiveSheet
'MsgBox mySheet.Name
mySheet.Name = "Austria"
Set db = CurrentDb
sSQL = db.QueryDefs("Booking_Curve_Austria").SQL
Set rs = db.OpenRecordset(sSQL)
For i = 1 To rs.Fields.Count
mySheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next i
mySheet.Cells(2, 1).CopyFromRecordset rs
'create graph
Range("A1:A8").Select
Range(Selection, Selection.End(xlToRight)).Select
Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("Austria").Range("A1:AB8"), PlotBy _
:=xlRows
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="BookingCurve"
With ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
'Save sheet
mySheet.SaveAs "P:\Documents\Austria_Booking_curve.xls"
'Close excel file
myBook.Close False
myExcel.Quit
Thanks
Dharmesh