Making graph - run-time error 424 - Object required - excel

I am trying to make a graph using the following VBa code. But I am getting a Run-time error 424 - Object required at the row:
Set rngChtXVal = .Columns(1).Offset(1).Resize(.Rows.Count - 1)
What is going wrong?
Sub MakeGraph()
Dim myChtObj As ChartObject
Dim rngChtData As Variant
Dim rngChtXVal As Variant
Dim iColumn As Long
sel= Sheets("Data").Range(Cells(1, 1), Cells(200, 2))
rngChtData = sel
' define chart's X values
With rngChtData
Set rngChtXVal = .Columns(1).Offset(1).Resize(.Rows.Count - 1)
End With
' add the chart
Set myChtObj = ActiveSheet.ChartObjects.Add(Left:=250, Width:=425, Top:=10, Height:=240)
With myChtObj.Chart
' make an XY chart
.ChartType = xlLine
' remove extra series
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
' add series from selected range, column by column
For iColumn = 2 To rngChtData.Columns.Count
With .SeriesCollection.NewSeries
.Values = rngChtXVal.Offset(, iColumn - 1)
.XValues = rngChtXVal
.Name = rngChtData(1, iColumn)
End With
Next
End With
End Sub

You've named rngChtData and rngChtXVal like Range objects, but you've declared them as Variant. While Variants can be Ranges, it makes debugging your code much harder. Use explicit variable types whenever possible. To resolve your issue, change your code like this:
Sub MakeGraph()
Dim myChtObj As ChartObject
Dim rngChtData As Range
Dim rngChtXVal As Range
Dim iColumn As Long
With Sheets("Data")
Set rngChtData = .Range(.Cells(1, 1), .Cells(200, 2))
End With
' define chart's X values
With rngChtData
Set rngChtXVal = .Columns(1).Offset(1).Resize(.Rows.Count - 1)
End With

Related

Excel VBA code for creating a chart object and plotting xyscatterline with data from different worksheets

I couldn't figure out what is wrong with the VBA code I created to plot data from worsheets that satisfying a condition on their name . xyscatter line to be created using data from column G as xvalues and column J as values.
I get error "Run-time error '1004': Application - defined or object-defined error"
Help is much appreciated as I will be doing this very often.
Thank you!
Sub charting()
Dim i, j, last_column, last_row As Integer
Dim sh As Worksheet
Dim cht As Chart
Dim xrng1, yrng1 As Range
Set cht = Charts.Add
i = 0
With cht
.ChartType = xlXYScatterLines
.Name = "cht1"
End With
For Each sh In ThisWorkbook.Worksheets
If LCase(sh.Name) Like "nov*" Or LCase(sh.Name) Like "bh*" Then
i = i + 1
With sh
last_row = .Cells(Rows.Count, 2).End(xlUp).Row
Set xrng1 = .Range(.Cells(2, 7), .Cells(last_row, 7))
Set yrng1 = .Range(.Cells(2, 10), .Cells(last_row, 10))
End With
End If
cht.Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(i).XValues = xrng1
ActiveChart.FullSeriesCollection(i).Values = yrng1
ActiveChart.FullSeriesCollection(i).Name = sh.Name
Next sh
End Sub

Adding new series to a graph automatically

I'm trying to create a dynamic Scatterchart in the worksheet("Graphs") using a button.
The Seriesname has to be equal to Worksheets("VS_P240_X").Cells(1,i), where i is a counter for the columns.
The XValues have to be equal to Worksheets("VS_P240_X").Range(cells(3,i).cells(1000,i)).
The YValues have to be equal to Worksheets("VS_P240_Y").Range(cells(3,i).cells(1000,i)).
When I Update the workbook the counter i will change, and I want that the chart will automatically update with the new series. I wrote this code but it is not working, do you have some suggestions?
Private Sub CommandButton5_Click()
'Graph generation NON COMPLETO
Dim i As Integer
Dim Chart1 As Chart
Set Chart1 = Sheets("Graphs").ChartObjects("Chart 1").Chart
For i = 1 To Lastcolumn
With Chart1
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
'Change to what your series should be called
.SeriesCollection(i).Name = Worksheets("VS_P240_X").Cells(1, i).Value
.SeriesCollection(i).XValues = Worksheets("VS_P240_X").Range(Cells(3, i), Cells(1000, i))
.SeriesCollection(i).Values = Worksheets("VS_P240_Y").Range(Cells(3, i), Cells(1000, i))
End With
Next i
End Sub
To avoid confusion/bugs, it's best to specify a worksheet every time you use Range or Cells()
Something like this should work:
Private Sub CommandButton5_Click()
Dim i As Long, Lastcolumn As Long 'use long not integer
Dim Chart1 As Chart, wsX As Worksheet, wsY As Worksheet
Set wsX = Worksheets("VS_P240_X")
Set wsY = Worksheets("VS_P240_Y")
Set Chart1 = Sheets("Graphs").ChartObjects("Chart 1").Chart
Do While Chart1.SeriesCollection.Count > 0 'remove any existing data
Chart1.SeriesCollection(1).Delete
Loop
Chart1.ChartType = xlXYScatter 'do this outside of the loop...
Lastcolumn = wsX.Cells(1, wsX.Columns.Count).End(xlToLeft).Column
For i = 1 To Lastcolumn
With Chart1.SeriesCollection.NewSeries
.Name = wsX.Cells(1, i).Value
.XValues = wsX.Range(wsX.Cells(3, i), wsX.Cells(1000, i))
.Values = wsY.Range(wsY.Cells(3, i), wsY.Cells(1000, i))
End With
Next i
End Sub

How to extend the range that is included in a graph with a macro

I have some data in column A and am trying to write a macro to extend the data range to the next - column B, so that every time I add data in following columns, the macro will extend the border to include that data in the graph plot.
See image below where only column A is included in my graph -
What do I need to do in order to get it to also include the next column - column B
For example:
What do you think?
This works for me:
Sub ExpandChartSource()
Dim ObjChart As Object
Dim RngSource As Range
Set ObjChart = ActiveSheet.ChartObjects(1)
Set RngSource = Range(Split(ObjChart.Chart.SeriesCollection(1).Formula, ",")(2))
Set RngSource = RngSource.Resize(RngSource.Rows.Count, RngSource.Columns.Count + 1)
ObjChart.Chart.SetSourceData Source:=RngSource
End Sub
Here a more dynamic version, useful if you want to enlarge the data indefinitely:
Sub ExpandChartSource()
Dim ObjChart As Object
Dim RngSource As Range
Dim IntSeries As Integer
Dim StrAddress As String
Set ObjChart = ActiveSheet.ChartObjects(1)
Set RngSource = Range(Split(ObjChart.Chart.SeriesCollection(1).Formula, ",")(2))
StrAddress = RngSource.Cells(1, 1).Address
Set RngSource = Range(Split(ObjChart.Chart.SeriesCollection(ObjChart.Chart.SeriesCollection.Count).Formula, ",")(2))
StrAddress = StrAddress & ":" & RngSource.Cells(RngSource.Rows.Count, 1).Address
Set RngSource = Range(StrAddress)
Set RngSource = RngSource.Resize(RngSource.Rows.Count, RngSource.Columns.Count + 1)
ObjChart.Chart.SetSourceData Source:=RngSource
End Sub
It assumes that the first series is the one most on the left while the last series is assumed to be on the right.

How to skip empty cells in a range to use as range for a scatterplot?

I'm fairly new to VBA and trying to dynamically select the data i need for my scatter plot. I'm trying skip any rows that have a blank in the "A" column so the scatter plot is continuous.
The code in the commented section is what i tried first, but that gives me a method range of object_global failed error on the setsourcedata line. I expect the code to skip the empty cell and continue through mydatatest variable until it reaches the end. However, the actual output stops at the first empty cell.
Set mydatatest = ActiveWorkbook.Sheets("Data - PLC").Range("A3:A94")
'Set mydata = ActiveWorkbook.Sheets("Data - PLC").Range("A2:C2")
Set mydata = ActiveWorkbook.Sheets("Data - PLC").Range("A2")
For Each mydatapoint In mydatatest
If IsEmpty(mydatapoint) = False Then
'Set mydata = Union(mydata, Range(mydatapoint, mydatapoint.Offset(0, 2)))
Set mydata = Union(mydata, mydatapoint)
End If
Next mydatapoint
ActiveWorkbook.Sheets("Report").Select
Set cht1 = Sheet1.ChartObjects.Add(10, 365, 275, 200)
With cht1.Chart
.ChartType = xlXYScatterLinesNoMarkers
.SeriesCollection.Add Source:=Range(mydata, mydata.Offset(0, 2))
'.SetSourceData Source:=Range(mydata)
End With
The error is in .SetSourceData Source:=Range(mydata). It should be .SetSourceData Source:=myData. myData is already declared as Range(). The rest is quite ok:
Option Explicit
Sub TestMe()
Dim myDataTest As Range
Dim myData As Range
Dim wks As Worksheet
Set wks = Worksheets(1)
Set myDataTest = wks.Range("A3:A94")
Set myData = wks.Range("A2")
Dim myDataPoint As Range
For Each myDataPoint In myDataTest
If Not IsEmpty(myDataPoint) Then
Set myData = Union(myData, myDataPoint)
End If
Next myDataPoint
Dim cht As Object
Set cht = wks.ChartObjects.Add(10, 365, 275, 200)
With cht.Chart
.ChartType = xlXYScatterLinesNoMarkers
.SeriesCollection.Add Source:=wks.Range(myData, myData.Offset(0, 2))
.SetSourceData Source:=myData
End With
End Sub

runtime error 91: Object variable or With block variable not set, example from MS office website

I am trying to extract data from MS-excel graph using macro given on MS excel website
Here is the code
Sub GetChartValues()
Dim NumberOfRows As Integer
Dim X As Object
Counter = 2
' Calculate the number of rows of data.
NumberOfRows = UBound(ActiveChart.SeriesCollection(1).Values)
Worksheets("ChartData").Cells(1, 1) = "X Values"
' Write x-axis values to worksheet.
With Worksheets("ChartData")
.Range(.Cells(2, 1), _
.Cells(NumberOfRows + 1, 1)) = _
Application.Transpose(ActiveChart.SeriesCollection(1).XValues)
End With
' Loop through all series in the chart and write their values to
' the worksheet.
For Each X In ActiveChart.SeriesCollection
Worksheets("ChartData").Cells(1, Counter) = X.Name
With Worksheets("ChartData")
.Range(.Cells(2, Counter), _
.Cells(NumberOfRows + 1, Counter)) = _
Application.Transpose(X.Values)
End With
Counter = Counter + 1
Next
End Sub
Where is the error in this code?
VB Complier will not know which ActiveChart you are talking about. Hence you need to set the chart object first and then try to use it.
Check this code:
Dim mychart As ChartObject
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
Set mychart = ws.ChartObjects("Chart 1")
With mychart.Chart
'do stuff here
End With

Resources