VBA Chart set a manuel Y-Axis Scale - excel

I wrote this method to create 2 charts:
Dim rng As Range
Dim cht As ChartObject
Dim pos As Range
Set rng = ActiveSheet.Range(data_range)
Set pos = Range(position)
Set cht = ActiveSheet.ChartObjects.Add( _
Left:=pos.Left, _
Width:=breite, _
Top:=pos.Top, _
Height:=hohe)
cht.Chart.HasTitle = True
cht.Chart.ChartTitle.Text = "Statistik"
cht.Chart.ChartTitle.Characters.Font.size = 11
cht.Chart.Legend.Delete
cht.Chart.SetSourceData Source:=rng
The problem is that when I use this method to create two charts, I have a different Y-Scale values (because of auto-scaling maybe) that makes the comparison difficult:
For this reason I need to set a same value for Y-Axis to have a better result.

You can try with
cht.Axes(xlValue, xlPrimary).MinimumScale = 0
cht.Axes(xlValue, xlPrimary).MaximumScale = 70

I should have added this lines to my code:
Dim yax As Axis
Set yax = cht.Chart.Axes(xlValue)
yax.Minimumscale = 100
Now works everything well.

Related

Excel VBA for making multiple graph without using Range

I want to have multiple series graph.
I made this code to show three lines.
However, it doesn't work, I can't use Range because my data source is sparse at three line ("I1:I30","I51:I80","I101:I131")
With ActiveSheet.Shapes.AddChart.Chart
.HasTitle = True
.ChartTitle.Text = "My Graph"
.ChartType = xlLine
.SetSourceData Range("I1:I30","I51:I80","I101:I131") 'thats the data for three lines I want to show.
.SeriesCollection(1).Name = "item1"
.SeriesCollection(1).XValues = Range("G1:G30")
How can I solve this?
The range setting is wrong.
Range("I1:I30","I51:I80","I101:I131")
The range should be set as follows.
Range("I1:I30, I51:I80, I101:I131")
However, if the range of the x-axis is constant, it will be appropriate to create a scatter chart.
Sub test()
Dim sourRange(1 To 3) As Range
Dim Srs As Series
Dim Cht As Chart
Dim i As Integer
Set sourRange(1) = Range("i1:i30")
Set sourRange(2) = Range("i51:i80")
Set sourRange(3) = Range("i101:i131")
Set Cht = ActiveSheet.Shapes.AddChart.Chart
With Cht
.HasTitle = True
.ChartTitle.Text = "My Graph"
.ChartType = xlXYScatterLinesNoMarkers
For Each Srs In .SeriesCollection
Srs.Delete
Next Srs
For i = 1 To 3
Set Srs = .SeriesCollection.NewSeries
With Srs
.Name = "item" & i
.Values = sourRange(i)
.XValues = Range("G1:G30")
End With
Next i
End With
End Sub
If the data is not in one contiguous range, then you need to add each series separately. It's the same when creating a chart manually with data in different parts of the worksheet. You cannot select a non-contiguous range and then insert a chart. Instead, create the chart with just one series, then use additional, separate commands to add more series to the chart.

Labeling last data points across multiple graphs and series

I'm currently trying to add a data label to only the last point of each series in each graph on a worksheet. I'm currently adapting one of the solutions proposed here: "https://superuser.com/questions/1285179/adding-data-label-only-to-the-last-value".
However, my code keeps on popping up with the error: "Runtime error '13', type mismatch". When I go in debug mode, it higlights the line "Set chrt = ws.ChartObjects(Chart_Name)".
Sub LastDataLabel()
Dim Chart_Name As String
Dim i, Total_Charts, Total_Series As Integer
Dim ws As Worksheet
Dim chrt As Chart
Dim srs As Series
Dim pnt As Point
Dim p As Integer
Application.ScreenUpdating = False
Set ws = ActiveSheet
Total_Charts = Range("C12").Value
For i = 1 To Total_Charts
Chart_Name = ActiveSheet.Cells(14 + i, 2).Value 'A list of all chart names exists along this range
Set chrt = ws.ChartObjects(Chart_Name)
Total_Series = chrt.SeriesCollection.Count
For j = 1 To Total_Series
Set srs = chrt.SeriesCollection(j)
srs.ApplyDataLabels
For p = 1 To srs.Points.Count - 1
Set pnt = srs.Points(p)
pnt.DataLabel.Text = ""
Next
srs.Points(srs.Points.Count).DataLabel.Format.TextFrame2.TextRange.Font.Size = 10
srs.Points(srs.Points.Count).DataLabel.Format.TextFrame2.TextRange.Font.Name = "Arial"
Next j
Next i
End Sub
Any and all help is greatly appreciated. Thank you!
ChartObject is just a container shape for the chart - it's not the actual chart itself
This should improve things:
Set chrt = ws.ChartObjects(Chart_Name).Chart

How to reference a dynamic multiple range?

I am trying to create a chart using a source data list (two separate columns) which can vary in column height depending on user inputs.
I am storing my dynamic value in a variable called "chartRange".
I originally made the sub using a fixed range, using a single code line, which works perfectly.
I replaced this single code line:
Set chartRange = ActiveSheet.Range("E8:E69,G8:G69")
with these 3 new lines of code:
Set r1 = Sheets("Sheet1").Range("E8":"E" & chartRange)
Set r2 = Sheets("Sheet1").Range("G8":"G" & chartRange)
Set myMultipleRange = Union(r1, r2)
I am trying to make these 3 new lines of code work, but getting error message.
Can someone help me correct the code problem?
I have included the full sub below.
Thanks in advance.
Dim chartRange As Integer
Dim cht As ChartObject
Dim totalPymts As Integer
Dim r1, r2, myMultipleRange As Range
' this variable is used to define table size
totalPymts = Worksheets("Sheet1").Cells(5, 4).Value
'my data range for the chart using single line
' Set chartRange = ActiveSheet.Range("E8:E69,G8:G69")
' the 3 new lines that dont work
Set r1 = Sheets("Sheet1").Range("E8":"E" & chartRange)
Set r2 = Sheets("Sheet1").Range("G8":"G" & chartRange)
Set myMultipleRange = Union(r1, r2)
'Create a chart
Set cht = ActiveSheet.ChartObjects.add( _
Left:=Range("a1").Left, _
Width:=450, _
Top:=Range("a5").Top, _
Height:=260)
'Give chart some data
cht.Chart.SetSourceData Source:=myMultipleRange
'Determine the chart type
cht.Chart.ChartType = xlLine
'Ensure chart has a title
cht.Chart.HasTitle = True
'Change chart's title
cht.Chart.ChartTitle.Text = "Repayments Schedual"
'Remove X-axis
' remove legend entry 1
Worksheets("sheet1").ChartObjects(1).Chart _
.Legend.LegendEntries(1).Delete
End Sub

how to move a pivot chart with vba

i am trying to make a function that creates a chart next to the pivot table but cant make the chart move =( display the right data and works fine, its just been created to far away from the actual pivot table.
Function chart_from_pivot(a_pivot As PivotTable) As Chart
Debug.Print 0
Dim objChart As Chart
Set objChart = Charts.Add
Debug.Print 1
objChart.SetSourceData a_pivot.TableRange1
Debug.Print 2
objChart.ChartType = xl3DColumn
Debug.Print 3
objChart.Location Where:=xlLocationAsObject, Name:=a_pivot.Parent.Name
Debug.Print 4
'HERE IS THE ISSUE!!!!
'objChart.Parent.Left = Range("D2").Left
'Debug.Print 5
'objChart.Parent.Top = Range("D2").Top
'Debug.Print 6
'objChart.ChartStyle = 294
'Debug.Print 7
chart_from_pivot = objChart
End Function
any recommendations? thanks guys.
I needed to set the objchart with the objchart.Location once i added the set now it works =)
this is the final code in case some one needed in the future.
Function chart_from_pivot(a_pivot As PivotTable) As Chart
Dim objChart As Chart
Set objChart = Charts.Add
objChart.SetSourceData a_pivot.TableRange1
objChart.ChartType = xl3DColumn
Set objChart = objChart.Location(Where:=xlLocationAsObject, Name:=a_pivot.Parent.Name)
objChart.ChartStyle = 294
objChart.ChartArea.Left = ThisWorkbook.Sheets("Charts").Range("B10").Left
objChart.ChartArea.Top = ThisWorkbook.Sheets("Charts").Range("B10").Top
chart_from_pivot = objChart
End Function

Changing the series in excel vba

I have a chart on one of my sheets and I need to change a series in it in code. The problem is that I keep getting the error 1004 message. I've looked around and can't find a reason for it. Here's the code:
Sheets("Charts").ChartObjects(1).Chart.SeriesCollection(1).Formula = "=G49:I" & dblResult & ")"
Are you trying this?
Sheets("Charts").ChartObjects(1).Chart.SeriesCollection(1).Formula = _
"=SERIES(,," & "Charts!G49:I" & dblResult & ",1)"
An alternative that I prefer to manipulating the Series Formula is to just work with the individual properties of the SeriesCollection.
Note I'm writing this from memory/untested so let me know if there's any problems with it!
Inside the With block, you would need to determine the Ranges to use for the Values, XValues, Name, and Order, of course you can omit the parts that you don't need (e.g., I rarely need to manipulate the series .Order)
Dim cht as Chart
Dim srs as Series '# Series variable'
Dim s as Long '# Series iterator'
Dim ws as Worksheet
Set ws = ActiveSheet
Set cht = ws.ChartObjects(1).Chart '## Modify as needed.'
For each srs in cht.SeriesCollection
With srs
s = s+1
.Values = ws.Range("Some_Range_For_Values")
.XValues = ws.Range("Range_For_XValues")
.Name = ws.Range("Range_For_SeriesName")
.Order = s
End With
Next
Practically speaking, here is a pretty simple example. I often build or update a chart dynamically using an approach like this. Assuming that XValues are in column A, while series data is in columns B:F, you could do something like:
Dim rngData as Range '# A range containing all of the series values & xValues'
Dim s as Long
Dim cht as Chart
Dim srs as Series
Dim ws as Worksheet
Set ws = ActiveSheet
Set cht = ws.ChartObjects(1).Chart
Set rngData = Range("A2:F10")
'## I like to remove existing series, and then add in the new data. '
For each srs in cht.SeriesCollection
srs.Delete
Next
'## Iterate over our range and add series back in to the chart.'
For s = 2 to rngData.Columns.Count
Set srs = cht.NewSeries
With srs
.XValues = rngData.Columns(1).Address
.Values = rngData.Columns(s).Address
'Name = rngData.Cells(1,s).Offset(-1,0).Value
'Order = s-1
End With
Next

Resources