Changing X axis scale in Excel - excel

I have a X-Y scatter plot, my values in the x axis starts from the value 30.
How can i change the scale in the chart in such a way that I have values in the X axis starting from 30 and not zero?

I was able to do this in VBA as follows:
grab the chart object:
Dim objChart As Object
Set objChart = Sheets("MyWorksheetName").ChartObjects("MyChartName")
change the x-axis min value:
objChart.Chart.Axes(xlCategory).MinimumScale = myChartMinXValue
The x-axis in Excel charts seem to be called xlCategory. Also NOTE the MinimumScale property doesn't exist for all chart types. My first attempt was using xlLine but I kept getting an error. Once I switched to xlXYScatterLines VBA was happy.

right click the x-axis
select Format Axis
select the Fixed option next to Minimum and enter 30

Related

How to hold the value of a variable in excel bar graph?

I have a plot in excel bar graph as shown in the image. But I want the Y axis data to be held at it's value until the next discrete sample in X axis .i.e the value of Y axis is 160.8 at X=1, and I want this value to be held until X=2 where the value of Y changes to 2. How do I do this?
Edit 1 - I tried following this tutorial and it worked - https://trumpexcel.com/step-chart-in-excel/
But I had to format the X axis cells to date format. I want the same to work when X axis format is a number.

How to get a scatter Chart Series MarkerBackgroundColor if marker filling is set to Automatic?

I have a scatter chart with multiple series and a trendline per series. I want to set the trendline datalable to be the same color as the series' fill color. The series' fill color is Automatic. The .MarkerBackgroundColor property returns -1 for all series while the .MarkerBackgroundColorIndex returns 2 for all series. I believe this only happens if the marker fill is set to Automatic. However, I can't change that because the series in the chart get added through another VBA code based on dynamic data. Any help?
For i = 1 To Sheet1.ChartObjects("Chart 5").Chart.SeriesCollection.Count
With Sheet1.ChartObjects("Chart 5").Chart.FullSeriesCollection(i)
.Trendlines(1).DataLabel.Format.Fill.ForeColor.RGB = .MarkerBackgroundColor
End With
Next i
In an XY Scatter chart with the marker fill set to "automatic", Excel will apply the colors in the same order as the six theme accent colors in the current palette.
If the chart has more than six series, a lighter/darker shade of these accent colors will be applied, again in the same order. How much lighter/darker actually varies with the Excel version. The screenshot shows the legend for the first 12 series and the default "Office" palette for Office 365 for your reference.
So, if you know the order in which the series sits in the chart, you can surmise its fill color.
Thanks teylyn.
Actually, I made a quick workaround by utilizing the .Trendlines(1).Format.Line.ForeColor.RGB instead of the .MarkerBackgroundColor and it surprisingly works although the Trendline color is set to Automatic as well!
Here is the edited code:
For i = 1 To Sheet1.ChartObjects("Chart 5").Chart.SeriesCollection.Count
With Sheet1.ChartObjects("Chart 5").Chart.FullSeriesCollection(i)
.Trendlines(1).DataLabel.Format.Fill.ForeColor.RGB = .Trendlines(1).Format.Line.ForeColor.RGB
End With
Next i

How auto scale the x-axis of a plot using VBA?

I'm trying to autoscale a plot in Excel, unfortunately, I only managed to scale the Y axis, because I get an error while setting the Maximum value of the X axis :
Run-time error '-2147467259 (80004005)':
Method 'MaximumScale' of object 'Axis' failed
I have got this code to help me scale my plot :
Option Explicit
Sub AutoScaleAxe()
With ActiveChart.Axes(xlValue, xlPrimary)
.MaximumScale = ActiveSheet.Range("E26").Value
.MinimumScale = ActiveSheet.Range("E25").Value
.MajorUnit = ActiveSheet.Range("E27").Value
End With
With ActiveChart.Axes(xlCategory, xlPrimary)
'The following line raises an error
.MaximumScale = ActiveSheet.Range("E22").Value
.MinimumScale = ActiveSheet.Range("E21").Value
.MajorUnit = ActiveSheet.Range("E23").Value
End With
End Sub
Here are the cells I use to scale the plot :
Your code works correctly if the chart type has a numeric horizontal axis - e.g. a scatter chart - but not if the horizontal axis is categorical.
If you want to display a histogram using a categorical type like a column chart, you'll need to either:
set up the chart with a fixed number of data points (i.e. fixed number of bins) and recalculate the actual data points (bin values) as the desired axis min and max are altered, or
calculate data points spanning the largest range of axis min to max that you want to cover, and alter the chart series data source range, e.g. ActiveChart.SeriesCollection(1).Values = myrange.
As this post explains, you can't use MinimumScale, MaximumScale of a chart with categorical X-axis.
The only thing that's available is TickLabelSpacing and TickMarkSpacing. Or, as #nekomatic explains, you should use numeric X-axis.

Excel VBA - Logarthmic x-axis

I am new to VBA. I am trying to plot a scatterplot using Excel VBA. I have chosen the x-axis to be plotted on a log scale. The issue is with the minimum value on the x-axis. I would like to have the y-axis plotted corresponding to 0.1. But it is being plotted corresponding to 1.
I tried to make the change using this piece of code
With .Axes(xlCategory)
.ScaleType = xlLogarithmic
.MinimumScale = 0.1
Still the probelm persists. I am able to shift the y-axis towards right but not to left of 1.
Please help to fix this issue.
Thanks,
Regards,
Kishor
I solved the above issue using CrossesAt. Below is the code
With .Axes(xlCategory)
.ScaleType = xlLogarithmic
.MinimumScale = 0.1
.CrossesAt = 0.1
End With

How to change axis value labels orientation?

I have a chart in Excel; I need to change orientation of text labels in one of the axis programmatically. Ideas?
This will change the orientation of the X-axis tick labels.
ActiveChart.Axes(xlCategory).TickLabels.Orientation = 45 ' degrees
This is how to change the orientation of the axis title:
ActiveChart.Axes(xlCategory).AxisTitle.Orientation = 81 ' degrees
Have you ever tried recording macros? If not, you should! Looking at the resulting code is a great way to quickly learn this type of thing.
Layout tab (which appears when you have a chart selected) -> labels -> axis titles.
or
Right click the chart axis -> format axis -> Alignment
or
VBA solution as Jean-François Corbett pointed out

Resources