Excel VBA can be soooo frustrating at times. And here is one. Why does this line not work? What do I need to do to fix it?:
ActiveChart.SetSourceData Source:=Sheets(2).range("B" & FindDataStart() & ":B" & FindEndRow())
The code runs fine as below (with the offending line commented out and a fixed value inserted):
ActiveChart.SetSourceData Source:=Sheets(2).range("B" & FindDataStart() & ":B" & FindEndRow())
The code runs fine as below (with the offending line commented out and a fixed value inserted):
Code:
Sub LineChartMaker2()
'
' ChartMaker2 Macro
'
Sheets("Line Chart").Activate
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
'ActiveChart.SetSourceData Source:=Sheets(2).range("B" & FindDataStart() & ":B" & FindEndRow())
ActiveChart.SetSourceData Source:=Sheets(2).range("B36" & ":B" & FindEndRow())
ActiveChart.SeriesCollection(1).Name = "=""Laser"""
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "=""R-Eye"""
'ActiveChart.SeriesCollection(2).Values = Sheets(2).range("t" & FindDataStart() & ":t" & FindEndRow())
ActiveChart.SeriesCollection(2).Values = Sheets(2).range("t36" & ":t" & FindEndRow())
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).AxisGroup = 2
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(3).Name = "=""L-Eye"""
'ActiveChart.SeriesCollection(3).Values = Sheets(2).range("q" & FindDataStart() & ":Q" & FindEndRow())
ActiveChart.SeriesCollection(3).Values = Sheets(2).range("q36" & ":Q" & FindEndRow())
ActiveChart.SeriesCollection(3).Select
ActiveChart.SeriesCollection(3).AxisGroup = 2
With ActiveChart.Parent
.Height = 325 ' resize
.Width = 1000 ' resize
.Top = 10 ' reposition
.Left = 10 ' reposition
End With
With ActiveChart
'chart name Top
.HasTitle = True
.ChartTitle.Characters.Text = "Laser Vs MobileEye for file: " & Sheets(2).range("b1").Value
ActiveChart.ChartTitle.Font.Size = 20
'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Data Pairs"
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Font.Size = 15
'y-axis name (Left hand side)
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Stripe Reflectivity"
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Font.Size = 15
'Secondary Y axis (Right hand side)
.Axes(xlValue, xlSecondary).HasTitle = True
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "MobileEye Values"
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Font.Size = 15
End With
End Sub
Function FindEndRow() As Integer
'Find end of data for chart
FindEndRow = Sheets(2).range("b65536").End(xlUp).Row
End Function
Function FindDataStart() As Integer
'Find start of data for chart
Dim rFound As range
On Error Resume Next 'cells(column, row)
With Sheet2
Set rFound = .range("B:B").find(What:="ave", After:=.Cells(2, 2), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
On Error GoTo 0
If Not rFound Is Nothing Then Application.Goto rFound, True
End With
'MsgBox ActiveCell.Address
FindDataStart = ActiveCell.Row + 1
End Function
Last edited by a moderator: