In Excel 365, I've just started making charts in VBA. I've never used charts much, manually or with VBA, but it works great for my current needs.
Trouble is, if I create a chart for, say, January to December, the resulting chart includes a previous December on the left and a following January on the right with no data. Is there a way to eliminate those empty spaces so that the data starts and ends at the left and right borders of the chart?
Chart adds extra category left and right
-
- 4StarLounger
- Posts: 401
- Joined: 31 Oct 2017, 20:07
-
- Administrator
- Posts: 77584
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 4StarLounger
- Posts: 401
- Joined: 31 Oct 2017, 20:07
Re: Chart adds extra category left and right
XY Scattering Lines
-
- Administrator
- Posts: 77584
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Chart adds extra category left and right
You can set the minimum and maximum values of the x-axis.
Either double-click the x-axis, or click once on the x-axis, activate the Format tab of the ribbon and click Format Selection.
Click Axis Options under Axis Options.
You'll see numbers in the Minimum and Maximum boxes, but you can enter a date in them, e.g. 1/1/2022, and press Enter.
Either double-click the x-axis, or click once on the x-axis, activate the Format tab of the ribbon and click Format Selection.
Click Axis Options under Axis Options.
You'll see numbers in the Minimum and Maximum boxes, but you can enter a date in them, e.g. 1/1/2022, and press Enter.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 401
- Joined: 31 Oct 2017, 20:07
Re: Chart adds extra category left and right
Perfect! Thanks Hans.
-
- 4StarLounger
- Posts: 401
- Joined: 31 Oct 2017, 20:07
Re: Chart adds extra category left and right
One little glitch, though. I can't seem to get the date conversion right.
I publicly declared lStart and lEnd as Long.
In the userform that gets the date input, I use this to convert the string date into the serial number:
So far so good.
But then in the calling macro I get an "Object doesn't support this property or method" error here:
I manually checked the serial numbers in the failed lines, and they are the correct dates.
I publicly declared lStart and lEnd as Long.
In the userform that gets the date input, I use this to convert the string date into the serial number:
Code: Select all
xDate = CDate(Me.txtStart)
lStart = CLng(xDate)
But then in the calling macro I get an "Object doesn't support this property or method" error here:
Code: Select all
cht.Chart.ChartType = xlXYScatterLines
cht.Axes(xlCategory).MinimumScale = lStart
cht.Axes(xlCategory).MaximumScale = lEnd
-
- Administrator
- Posts: 77584
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Chart adds extra category left and right
I assume that cht is a ChartObject. If so, use
cht.Chart.Axes(xlCategory).MinimumScale = lStart
cht.Chart.Axes(xlCategory).MaximumScale = lEnd
just like you used cht.Chart in the line
cht.Chart.ChartType = xlXYScatterLines
cht.Chart.Axes(xlCategory).MinimumScale = lStart
cht.Chart.Axes(xlCategory).MaximumScale = lEnd
just like you used cht.Chart in the line
cht.Chart.ChartType = xlXYScatterLines
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 401
- Joined: 31 Oct 2017, 20:07
Re: Chart adds extra category left and right
Of course. Sometimes I miss what's right in front of me. As always, many thanks!