Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Graph once but not twice

Status
Not open for further replies.

km2x

MIS
Jun 21, 2002
5
0
0
US
I am creating a graph in Excel that has access data pushed into a group of cells. When I run the code the first time it works great but when I close excel and re-run I recieve errors. Also if I leave excel open and rerun there is not a problem since it creates a duplicate graph once again.

Any help would be greatly appreciated!

Public Sub Help1()


Dim txtfilename As String
Dim sState As String

Dim objExcel As New Excel.Application '********* REMEMBER TO REFERENCE THE EXCEL LIBRARY BY SELECTING "TOOLS MENU, REFERENCES..." AND CHECKING "Microsoft Excel 9.0 Object Library"

With objExcel
.Workbooks.Add 'add a new workbook
.DisplayAlerts = False

.Visible = True 'show Excel - you can remove this if you don't want the user to see whats happening

'remove all of the sheets
While .Sheets.Count > 1
.Sheets(1).Delete
Wend

'create four sheets of data

sState = "SELECT * FROM TrendAnalog_1 WHERE Name = 8133"


AddSheet objExcel, "Data Sheet", "Data for Graph", sState, .Sheets(1)

' AddSheet objExcel, "SHEET 2", "FX Data from Query", "Query1"
' AddSheet objExcel, "Event Log", "Event Log Data", "SELECT * FROM Table2"
'AddSheet objExcel, "EMAIL Addr", "Email Addresses", "SELECT * FROM Table3"


'save the excel sheet
' .ActiveWorkbook.SaveAs txtfilename

'close the workbook
'.ActiveWorkbook.Close

.DisplayAlerts = True
'.Quit 'quit Excel
End With

'release the object




'MsgBox "An excel spreadsheet has been created at: " & txtfilename & vbCrLf & vbCrLf & "This file can now be attached to an Email using SendObject.."

ActiveWindow.SmallScroll Down:=-15
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Sheets("Data Sheet").Range("B4:C255"), _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Trend Line"
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
ActiveChart.HasLegend = False
ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowNone, LegendKey:=False
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear, Forward:=0, _
Backward:=0, DisplayEquation:=False, DisplayRSquared:=False).Select


Set objExcel = Nothing




End Sub




Private Function AddSheet(ByRef objExcel As Excel.Application, ByVal sSheetName As String, ByVal sTitle As String, ByVal sSQL As String, Optional ByRef objSheet As Excel.Worksheet)

Dim objSht As Excel.Worksheet
Dim objRecordset As Recordset
Dim nCount As Integer

If objSheet Is Nothing Then
Set objSht = objExcel.Sheets.Add
Else
Set objSht = objSheet
End If

'get the data to put in the sheet
Set objRecordset = CodeDb.OpenRecordset(sSQL)

With objSht
.Cells(4, 1).CopyFromRecordset objRecordset 'paste the data into the sheet

'add the field names
For nCount = 0 To objRecordset.Fields.Count - 1
.Cells(3, 1 + nCount).Formula = objRecordset.Fields(nCount).Name
.Cells(3, 1 + nCount).Font.Bold = True
Next nCount

.Columns.AutoFit 'autofit the data

'add the title
.Cells(1, 1).Formula = sTitle
.Cells(1, 1).Font.Bold = True
.Cells(1, 1).Font.Size = 14

'set the sheet name
.Name = sSheetName
End With

Set objRecordset = Nothing


End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top