I have a graph that has dates on the x-axis and I'm trying to set maximum and minimum values for this axis using an Excel VBA. I defined MinXAxis and MaxXAxis values in the same sheet and here is my code:
Sub UpdateChartAxes()
With ActiveSheet.ChartObjects("Chart 1").Chart
.Axes(xlCategory).MinimumScaleIsAuto = False
.Axes(xlCategory).MinimumScale = Range("MinXAxis").Value
.Axes(xlCategory).MaximumScaleIsAuto = False
.Axes(xlCategory).MaximumScale = Range("MaxXAxis").Value
End With
End Sub
When I run this code I get error 400 with no explanation about the error.
Any ideas what might be going wrong?
Axis.MinimumScale Property
Returns or sets the minimum value on the value axis. Read/write
Double.
You should be applying these properties to the value axis xlValue, not the Category axis.
Setting this property will also set MinimumScaleIsAuto = False so you won't need those lines.
Added If you use a column, bar, line, etc., graph then these have a Value and a Category axis. You can only set the Minimum or Maximum for the Value axis. Even if you swap them around (x - y) there is still only one Value and one Category axis. Edited An exception to this is if dates are used as the Category axis, in which case Excel enables the Scale settings, as it recognises them as values.
If, instead, you use a Scatter graph, then this has both X-Values and Y-Values, and each of these value axes can be given maxima and minima.
By far the easiest way to prove all this is to double click on an axis and see whether the Axis Options allows you to set a minimum and maximum.
At the beginning of the code, add
On Error GoTo ShowErrDescription.
After End With and before End Sub, add the following code
Exit Sub
ShowErrDescription:
MsgBox Err.Description
This should at least give you a little more information about the error.
Related
Is there a way to set the visible x-axis range in JSChart despite the range of the data set?
I use JSChart to draw a line chart. The x-axis is date. The y-axis is an integer value (from 0 to 100).
Let's save x-axis min value is 2000-1-1 and max value is 2010-12-31).
Is there way way to configure JSChart to show only the part of data say (2005-1-1 to 2006-1-1)?
I have tried doing
config.data = myData
config.options.scales.x.suggestedMin = start
config.options.scales.x.suggestedMax = end
where start is 2005-1-1 and end is 2006-1-1. But JSChart still show my data from the beginning, i.e. 2000-1-1.
see this ref:
suggestedMin only change the data values that are used to scale the axis.
To change the explicit ends of the axis, use min and max.
I'm trying to change it from based on data to text. how can i write it programmatically?
by changing reverse plot order to false and category minimum solves my problem.
'Hence the name, reverse plot order
.Axes(xlCategory).ReversePlotOrder = False
'and keep crosses minimum (this is "between dates" in Excel GUI Format Axis -> Axis Options)
.Axes(xlCategory).Crosses = xlAxisCrossesMinimum
I am producing a VBA subroutine in an Access database which generates an Excel File however when I modify the column widths using:
Range("A:A").ColumnWidth = ...
Produces mixed results depending on the unit of measurement. Excel's default measurement set in options is Centimeters. I have tried entering the value in points and in cm but the result is either too wide or too small (see below):
Range("A:A").ColumnWidth = 3.07 'In Centimeters - Too small
Range("A:A").ColumnWidth = 87.02 'In Points - Too big
According to the documentation
Range.ColumnWidth
is set using the measurement in the points unit of measurement (width of 0). Regardless of the value I enter the result isn't the same.
The .ColumnWidth property refers to the number of zeros you can type in a cell in the default font without exceeding the cell's width.
In a new workbook where presumably your default column width is 8.43, type '000000000 in a cell and you'll see that the 9th zero overflows the right cell border by about half a zero.
To set the cell width in points, set .ColumnWidth to points / cell.Width * cell.ColumnWidth. Here's the catch, you have to set it three times to get it close, but it will almost never be exact. So put the .ColumnWidth assignment in a For Next loop and set it three times.
Note that the .Width property is in points and you can't set it. The .ColumnWidth property is in crazy-zero-width-measurement and you can set it. Why it takes three times, I don't know.
See http://dailydoseofexcel.com/archives/2004/06/01/column-widths-in-points/ for some test results on the 'three times' thing.
I have a gui that provides min and max values for a selected axis, like the following for the x-axis:
'X-axis
If ListBox8.ListIndex = 0 Then
ActiveChart.Axes(xlCategory, xlPrimary).MaximumScale = TI_Chart_Tool.MultiPage1.Pages(2).TextBox2.Text
ActiveChart.Axes(xlCategory, xlPrimary).MinimumScale = TI_Chart_Tool.MultiPage1.Pages(2).TextBox3.Text
End If
At the time this occurs, the x-axis of the chart, should change accordingly, right? Initial passes, for both x and y axis, seem to work fine, while testing, i seemed to get into a state such that the chart values would only be updated, when i clicked on the chart area.
(Let me add, this state seems to occur after i change the y-axis to a logarithmic scale.)
Does anyone know how to always have the axis values be updated automatically, from within the code without having to click on the chart?
Thanks for any help!
Russ
In previous versions of Excel there was a registry entry that you could create to allow Excel to display values/labels that would be positioned outside the axis min/max using QFE_Bonn dword=1. This is what I have used for Excel 2003: Plot lines that contain labels disappear ...)
I have not been able to find a similar patch or native functionality in Excel 2010 (Office Pro Plus). Any ideas how this can be accomplished, or did MS remove this functionality altogether?
Here are screenshots of examples in Excel 2003. I create a series of data which uniformly exceeds the y-axis maximum. This series' color fill has been removed already
To finish the look, remove the series' border so that it appears invisible. Then replace the series' value labels with the relevant data.
There is a workaround using the DataLabels.Left property which positions the DataLabel relative to the ChartArea.
Here is an example VB solution:
sub FakeLabels()
Dim sF As Double
Dim lOff As Double
Dim p As Double
ActiveSheet.ChartObjects(1).Activate
With ActiveChart
For sF = 1 To .SeriesCollection.Count
If .SeriesCollection(sF).Name = "FakeSeries" Then
'Define the lOff variable by adding 100, or some other value
lOff = .SeriesCollection(sF).Points(1).DataLabel.Left + 100
For p = 1 To .SeriesCollection(sF).Points.Count
.SeriesCollection(sF).Points(p).DataLabel.Left = lOff
Next p
End If
Next sF
End With
It yields the same results, the only new requirement is to keep the values for the "dummy" series within the axis min/max values for the chart.
A pleasant surprise is that re-sizing the chart doesn’t appear to affect the relative placement of the labels.
UPDATED 9-25-2013
I have used the "textbox" approach since first asking this question. But it is extremely clunky to manage the interplay between the labels' position and the textbox positions, their relative position of the PlotArea i.e., when to use .InsideWidth vs. .Width or .InsideLeft vs. .Left and whether there needs to be any sort of hedonic "adjustments" to the points values, as always seem to be the case, they are never quite perfectly aligned.
While perusing the PPT object model reference for some other chart-related inquiries, I stumbled upon this property which appears to replicate the functionality of the previous hotfix/registry hack.
.ShowDataLabelsOverMaximum