Article ID: 112417
Article Last Modified on 7/15/2004
Sub Command1_Click()
Dim objXLsheet As Object ' Object reference to Excel Worksheet
Dim objRange1 As Object ' First series in the chart
Dim objRange2 As Object ' Second series in the chart
Dim objChart1 As Object ' Object reference to the chart we create
Dim iRow As Integer ' Index variable for the current Row
Dim iCol As Integer ' Index variable for the current Row
Dim strTmpRange As String ' Temporarily hold Range in R1C1 notation
Const cNumCols = 10 ' Number of points in each Series
Const cNumRows = 2 ' Number of Series
' Create a Worksheet Object:
Set objXLsheet = CreateObject("Excel.Sheet")
Randomize Timer
' Insert Random data into Cells for the two Series:
For iRow = 1 To cNumRows
For iCol = 1 To cNumCols
objXLsheet.Cells(iRow, iCol).Value = Int(Rnd * 50) + 1
Next iCol
Next iRow
' Insert Named Ranges:
For iRow = 1 To cNumRows
'VB3Line: Enter the following lines as one line
strTmpRange = "R" & iRow & "C" & Format$(1) & ":R" & iRow &
"C" & Format$(cNumCols)
'VB3Line: Enter the following lines as one line
objXLsheet.Parent.Names.Add "Range" & Format$(iRow),
"=Sheet1!" & strTmpRange
Next iRow
' Add a ChartObject to the worksheet:
Set objChart1 = objXLsheet.ChartObjects.Add(100, 100, 200, 200)
' Assign the Ranges created above as the individual series
' for the chart:
For iRow = 1 To cNumRows
objChart1.Chart.SeriesCollection.Add "Range" & Format$(iRow)
Next iRow
' Make Excel Visible:
objXLsheet.application.Visible = True
DoEvents
' Save the Worksheet to disk. The parent of a WorkSheet is WorkBook.
objXLsheet.Parent.SaveAs "C:\VB\XLCHART.XLS"
' Close this instance of Excel:
objXLsheet.application.Quit
End Sub
Keywords: kbhowto kbprogramming KB112417