Application Defined Error in Excel VBA
Sub CreateChart2()
Dim cntr As Integer
Dim objChart As ChartObject
Dim myChtRange As Range
Dim ws As Worksheet
Dim wb As Workbook
Set wb = Workbooks.Open("c:\mahanti\graph.xls")
Set ws = wb.Sheets(2)
Dim left, top, width, height
left = 100
top = 40
width = 340
height = 250
With ws
For cntr = 1 To 7
Set objChart = .ChartObjects.Add( _
left:=left, top:=top, width:=width, height:=height)
MsgBox "add"
With objChart.Chart
ws.Activate
wb.Sheets(2).ChartObjects(cntr).Select
.ChartArea.AutoScaleFont = False
.ChartType = xlXYScatterLines
' Charts("Chart1").Activate
MsgBox "activate"
Dim i As Integer
Dim x1 As Integer, y1 As Integer
x1 = 2
y1 = 4
For i = 1 To 10
MsgBox "infor"
.SeriesCollection.NewSeries
MsgBox "series"
.SeriesCollection(i).Name = "=Sheet1!R" & x1 & "C2"
.SeriesCollection(i).XValues = Worksheets("Sheet1").Range("a" & x1 & ":a" & x1 + 2)
MsgBox "x series"
.SeriesCollection(i).Values = Worksheets("Sheet1").Range(Chr(66 + cntr) & x1 & ":" & Chr(66 + cntr) & x1 + 2)
' while executin the above line I am geeting 400 error
MsgBox "y series"
x1 = x1 + 5
Next i
.HasTitle = True
.ChartTitle.Characters.Text = "My Title"
.ChartTitle.Font.Bold = True
.ChartTitle.Font.Size = 12
With .Axes(xlCategory, xlPrimary)
.HasTitle = True
With .AxisTitle
.Characters.Text = "users"
.Font.Size = 10
.Font.Bold = True
End With
End With
With .Axes(xlValue, xlPrimary)
.HasTitle = True
With .AxisTitle
.Characters.Text = Worksheets("sheet1").Range(Chr(66 + cntr) & "1")
.Font.Size = 10
.Font.Bold = True
End With
End With
End With
top = top + 300
Next cntr
End With
End Sub
|