Macro to format y- axis in Excel - excel

I've written some code to create a second y-axis in a chart. I recorded a macro when I formated the second y-axis and the code looks great. However, it doesn't work when I try to run it from the VBA code..
I've tried the Format Painter (the first y-axis), which would be optimal if it worked, and also by formating the second y-axis "by hand"
This is what I have (and it's doesn't work..)
Formating the second y-axis to Calibri ("by hand"):
ActiveChart.Axes(xlValue, xlSecondary).Select
Selection.Format.TextFrame2.TextRange.Font.Name = "+mj-lt" 'This line doesn't work...
Selection.Format.TextFrame2.TextRange.Font.Size = 14
Error code: Method TextFrame2 of Object 'ChartFormat' failed
Using Format Painter:
ActiveChart.Axes(xlValue).Select
Selection.Format.PickUp 'This line doesn't work...
ActiveChart.Axes(xlValue, xlSecondary).Select
Error code: Object doesn't support this property or method.
Does anyone know why none of the methods work or if there's a work around?

I found a work around by using:
With ActiveChart.Axes(xlValue, xlSecondary).TickLabels.Font
.Name = "Calibri"
.Size = 16
End with
I haven't been able to solve the issue with Format Painter though...

Related

Excel histogram bins adjustment with VBA doesn't seem to work

I'm trying to automatically create couple histograms for given data. The problem is that I can't change bins width with VBA. How to get around this?
(Excel Version 2210 Build 16.0.15726.20188)
I looked through the docs and tried the macro recorder and this is prototype code I came up with.
Sub generateHistograms()
Dim wsTab As Worksheet
Set wsTab = Worksheets("Tabele")
With wsTab.Shapes.AddChart2(-1, xlHistogram)
.Name = "Test"
.Chart.SeriesCollection.NewSeries
.Chart.FullSeriesCollection(1).Values = wsTab.ListObjects(1).ListColumns(3).DataBodyRange
.Chart.ChartGroups(1).BinsType = xlBinsTypeManual
.Chart.ChartGroups(1).BinWidthValue = 10
.Chart.ChartGroups(1).BinsOverflowEnabled = True
.Chart.ChartGroups(1).BinsOverflowValue = 90
End With
End Sub
Running the code line by line seems like BinsType, BinWidthValue, BinsOverflowEnabled and BinsOverflowValue doesn't do anything.

How to change the subtype of Chart from VBA code

I create charts from some data:
ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select
But on Excel there are different subtypes of the xlColumnStacked chart...
I need to show the second one, not the default one.
It has to be programmatically, not manually.
I tried to record the macro and see what does the change, but it doesn't record anything... I tried loading from Templates also but they don't work exactly like the one I need.
Thanks for your support.
I thing you have to change .ChartTyp
for example:
ActiveChart.ChartType = 52
Here is list of proper Value
https://bettersolutions.com/excel/charts/vba-chart-types.htm
If you want to switch x axis and y axis data try one of this:
ActiveChart.PlotBy = xlColumns
ActiveChart.PlotBy = xlRows
I hope I helped :)
They both looked like stacked columns. VBA doesn't have chart subtypes, and controls the chart type using this syntax:
ActiveChart.ChartType = xlColumnClustered
ActiveChart.ChartType = xlColumnStacked
ActiveChart.ChartType = xlColumnClusteredStacked100
VBA's Object Browser shows you all of the possibilities.

Change data range in a chart using VBA

It has been hours that I'm struggling with what I think to be a sible problem since Im not familiar at all with Chart object in VBA.
Here's my chart:
What I want to do: Change the data range of the two axis, the problem is that cant figure out a way to edit the series of the chart.
Thanks four your help !
You could use
With Workbooks(bookname).Charts(Chartname)
.SeriesCollection.NewSeries
.SeriesCollection(i).XValues = YourXs
.SeriesCollection(i).values = YourYs
end with
You can choose which series you want to edit by using the index i. This actually sets the (X,Y) pairings while what's below only changes the range shown by the graph.
To change the bounds of your Y axis you can use
.Axes(xlValue).MinimumScale =
.Axes(xlValue).MaximumScale =
To change the bounds of your x axis use
.Axes(xlCategory).MinimumScale =
.Axes(xlCategory).MaximumScale =
You said you wanted to change
=SERIES("SO2 U5",'CEMS_U6_YTD 2018'!$B$2165:$B$2303,'CEMS_U5_YTD 2018'!$D$2165:$D$2312,1)
to this
=SERIES("SO2 U5",'CEMS_U6_YTD 2018'!$C$2165:$C$2303,'CEMS_U5_YTD 2018'!$D$2165:$D$2312,1)
This may be a simple as
ActiveChart.SeriesCollection(1).Formula = _
"=SERIES(""SO2 U5"",'CEMS_U6_YTD 2018'!$C$2165:$C$2303,'CEMS_U5_YTD 2018'!$D$2165:$D$2312,1)"
note doubling of the double quotes around the series name.
However, since you are only changing the X values, you could use this:
ActiveChart.SeriesCollection(1).XValues = "='CEMS_U6_YTD 2018'!$C$2165:$C$2303"
or
ActiveChart.SeriesCollection(1).XValues = Worksheets("CEMS_U6_YTD 2018").Range("$C$2165:$C$2303")
I've written a tutorial about editing series formulas with VBA: Change Series Formula – Improved Routines.

Accessing PivotChart SeriesCollection with a Sub

I am quite new to VBA. I have written a macro which creates about 10 pivot charts and some normal charts after filtering and cutting some data from a database spreadsheet. I now want to write a sub which goes through applying the same formatting to each one. The sub is as follows:
Sub FormatChart(Cht As ChartObject, title As String)
Cht.Activate
MsgBox ActiveChart.SeriesCollection.Count
With ActiveChart
.HasTitle = True
.ChartTitle.Text = title
.Axes(xlValue).HasMajorGridlines = False
End With
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 182, 0)
End With
End Sub
I originally didn't include all the activates and selects, but couldn't get the macro to work without them and don't see it as the end of the world - the datasets are never going to be massive so speed isn't so much of a concern, and I disable screenupdating so that users can't click on cells/other objects and disrupt the macro as it runs.
Here's my problem. If I take out the second With loop, everything proceeds perfectly and the gridlines are removed and the title is added. However, whenever I try to edit the colours of columns with the above I get Run time error '1004': Invalid parameter. I've also tried keeping the content of the second with loop inside the first but then moved it out to try using selection to see if it made a difference.
I've fiddled around quite a bit and recorded various macros changing the colour of the chart in question, but I think the problem might be to do with referencing SeriesCollection as when I try to debug with
MsgBox ActiveChart.SeriesCollection.Count
I get "0".
Please let me know if I'm missing the point - as I said I am new to VBA and am trying to learn as much as possible :)
EDIT: The solution was that I was passing each chart to the above sub after I had created the chart, but before I had set a data source for the chart. Doh!
This obviously meant that there were no seriesCollections for that chart, hence the error I was getting.
I marked Joehannah as answering the question (even though it isn't technically the solution) because it made me check my code and notice that the above could be causing the problem - if I shouldn't do that someone please tell me and I'll try to fix it!
You are better off using Set cht = (create chart object) for each chart and then Immediately calling the format method passing in cht.
I have been told to post my own answer since I figured out the issue.
I was passing each chart to this function just after creating it, as follows:
Set ptr1 = Sheets("withinBRSPivots").PivotTables("UniqueClicks").TableRange1
Set cht1 = Sheets("BRS Overview").ChartObjects.Add(Left:=950, Top:=500, _
Width:=400, Height:=300)
cht1.Name = "UCChart"
Charter.FormatChart cht1, "Average Number of Unqique Clicks"
I then set the source data for the chart after doing the above. This meant that when the chart was passed to my chartformat sub, it had no source data. This is what resulted in being able to edit the title and gridlines, but not the seriesCollection. The fix was to just move the FormatChart sub line to after I had set the source data.
Many thanks to everyone who posted answers!

VBA run-time error "7" when naming xlXY ScatterLines chart

I'm running a code that requires me to name a chart so I can refer back to it later. The code I use for this is:
Sheets("Summary").Activate
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
With ActiveChart
.Name = "energy distribution chart"
End With
I've tried just naming the chart not using the with... as in:
ActiveChart.Name = "energy distribution chart"
Doesn't work also. Whenever the code runs at that line I get a "run-time error 7 - Out of memory". I've tried putting this code so that it starts to run in a new macro in a new module but it still always gets that error 7 - 3 lines into a new piece of code! I have no idea what's going on. Help?
Edit: Following to mehow's answer, how do I then re-select the chart?
I've tried:
With Worksheets("Summary").ChartObjects("energy distribution chart").Chart(1)
.SeriesCollection(sheet_number - 1).XValues = Worksheets(sheet_number).Range(Sheets(sheet_number).Cells(1, 7), Sheets(sheet_number).Cells(i4, 7))
.SeriesCollection(sheet_number - 1).Values = Worksheets(sheet_number).Range(Sheets(sheet_number).Cells(1, 8), Sheets(sheet_number).Cells(i4, 8))
End With
Which doesn't seem to work... .. hm. I've also tried getting rid of the (1) next to charts. Yep. No idea.
Chart it's an embed object. If you want to change of the object ( not chart itself ) then
ActiveChart.Parent.Name = "chart name"
See MSDN for more details

Resources