Keep Chart Data Label from Wraping - excel

I have a project where I have to put user generated data labels in a chart. These need to be on a series line as such:
![What I want format][1]
However in some instances the text wraps. How do I keep the text from wrapping in excel 10? see below
![enter image description here][2]
'-------- FORMAT DATA LABELS -------------------------------------
ActiveSheet.ChartObjects("OperBal2Takt").Activate
h = 0
For h = 2 To 7
With ActiveChart.SeriesCollection(h).Points(1)
.HasDataLabel = True
.DataLabel.Text = Cells(2018 + h, 8).Value
'.DataLabel.Width = msofit
End With
Next h

In Excel 2013, the size of your data label can be changed. In previous versions, the label cannot be resized.
In earlier Excel versions, sometimes I've stretched the chart to make it wider, but shrunk the plot area of the chart down to the original size, so the white space is wider on both sides of the chart. This works because the maximum size of a label is proportional to the chart width.
Other times I've replaced the data label with a text box, which can be flexibly resized.

use this code:
ChartObj.chart.seriescollection(1).datalabels.Format.TextFrame2.WordWrap = msoFalse
and also, but less usefull:
ChartObj.chart.seriescollection(1).datalabels.Format.TextFrame2.AutoSize = msoAutoSizeNone

Related

How to use Conditionally Formatted Data bars on three leveled grouped rows that are populated with data on the fly

I have a situation where there are rows grouped at three levels. The data is populated dynamically. Currently, I am able to assign conditionally formatted data bars across the whole column (Col Q). But, I want different colored data bars for each level of rows. See in the picture below, I want data bars for all red colored rows to be in red color (Row 47), data bars for purple colored rows to be in purple color. What I have right now is purple all the way.
* UPDATE *
This is the code block I have for all the red rows. Somehow, the bar size is not reflective of the value present in the corresponding cell.
Set db = Worksheets("Deliverable-Epic-Story Progress").Cells(delvRow, delv_StatusCell + 1).FormatConditions.AddDatabar()
db.BarColor.Color = vbRed
db.MinPoint.Modify newtype:=xlConditionValuePercent, newvalue:=0
db.MaxPoint.Modify newtype:=xlConditionValuePercent, newvalue:=100
Modified the code block to this. I added the format via code. Changed the min and max values.
Cells(delvRow, delv_StatusCell + 1).NumberFormat = "0%"
Set db = Worksheets("Deliverable-Epic-Story Progress").Cells(delvRow, delv_StatusCell + 1).FormatConditions.AddDatabar()
db.BarColor.Color = vbRed
db.MinPoint.Modify newtype:=xlConditionValuenumber, newvalue:=0
db.MaxPoint.Modify newtype:=xlConditionValuenumber, newvalue:=1

How to avoid "A Chart may only have up to 256 series" issue (Excel VBA)

I'm trying to build bubble chart from a scratch in excel. Below is piece of code I'm using. Everything works fine until SeriesCollection.NewSeries reaches 256. Then I'm getting error message: A Chart may only have up to 256 series.
Can you anybody help me, how to avoid this issue. Is there any more elegant solution? Thanks a lot for any of your hint or advice.
Source code for my bubble chart:
With wsAnalytics.ChartObjects.Add(Left:=0, Width:=995, Top:=0, Height:=580)
For i = 1 To Range("Config_BubbleChartData").Rows.Count
.Chart.SeriesCollection.NewSeries
.Chart.SeriesCollection(i).name = Range("Config_BubbleChartData").Rows(i).Cells(1, 1)
.Chart.SeriesCollection(i).XValues = Range("Config_BubbleChartData").Rows(i).Cells(1, 3)
.Chart.SeriesCollection(i).Values = Range("Config_BubbleChartData").Rows(i).Cells(1, 4)
Select Case BubbleColor
Case 1
.Chart.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
Case 2
.Chart.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(0, 0, 255)
Case 3
.Chart.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
End Select
If i = 1 Then .Chart.ChartType = xlBubble3DEffect
.Chart.SeriesCollection(i).BubbleSizes = "=" & Range("Config_BubbleChartData").Parent.name _
& "!" & Range("Config_BubbleChartData").Rows(i).Cells(1, 5).Address(1, 1, xlR1C1)
Next i
You only need as many series as you have colors. Certainly you don't need more than 255 colors, since no human would be able to distinguish more than a dozen in a cluttered bubble chart.
Set up the data with four columns for X, Y, bubble size, and bubble color. Append another column for each color in the bubble color column, and use the color name as the column header. Use an IF formula in these columns to put the Y value for each color into the appropriate column.
In my example, I have X and Y in columns A and B, bubble size and color in C and D, plus the bubble color names in E1:G1. The formula in cell E2 is
=IF($D2=E$1,$B2,NA())
and I've filled this into E2:G16. The result is the Y value if the column header matches the color, and #N/A otherwise, which will not plot.
Select A2:C19 and create the bubble chart (top left chart).
Click the Select Data button on the Chart Tools > Design tab of the ribbon, select the only series in the chart, and Edit it, so cell E1 is used for the name, and E2:E16 is used for the Y values. Keep A2:A16 as the X values and C2:C16 as the bubble size. This series will have the default color for the first series, which in Excel 2013 and 2016 is blue (top right chart).
Stay in or return to the Edit Data dialog, and add a series. Use cell F1 for series name, A2:A16 as X values, F2:F16 as Y values, and C2:C16 as the bubble size. This series will use the default second color, orange (bottom left chart).
Add one more series, use cell G1 for series name, A2:A16 as X values, G2:G16 as Y values, and C2:C16 as the bubble size. This series uses the default third color, gray (bottom right chart).
Note that all series use the same X values and bubble sizes, but different Y values.

How to create a text based Y axis on excel chart

I wish to customized the Y-axis to text instead on value.
Is there any way I can do it without any plugin?
Here will be my data:
A B C D
1 Lower Limit Upper Limit # Data
2 30S 40S A1234 30A
3 30S 40S A2345 30S
4 30S 40S A1256 30S
I wish my Y axis will be 30S, 30A, 40A, 40S intead of 10,20,30,40
and my X axis will be A1234,A2345,A1256
Is there any possible that I can change the X axis value?
I started with your data, and I added a small lookup table. I inserted a column "Value" (blue text) in your data range that has the Y value corresponding to the label in your "Data" column. You can use a lookup formula for this, for example, the formula in D2 is:
=INDEX($H$2:$H$5,MATCH(E2,$I$2:$I$5,0))
I made a column chart (don't know what type of chart you want, but this technique is widely applicable) using the yellow shaded range in the first data range (top left chart). I selected and copied the green shaded range in the second data range, selected the chart, and used Paste Special to add the data to the chart, using the settings shown in the dialog overlying the top right chart.
This has added another set of columns (bottom left chart), not so useful. I right-clicked the added series, picked Change Series Chart Type from the pop-up menu, and changed just this series to an XY scatter type (bottom right chart).
The XY scatter series is on the secondary axis, also not too useful. I formatted the XY series, and changed the axis to Primary (top left chart below). The 0.5 values in column G make the points line up with the vertical axis of the chart.
I added data labels to the XY series, shown in orange to distinguish them from the black axis labels (top right chart below). I formatted the labels so they appear to the left of the points, and so they used the Value from Cells option, based on the labels in I2:I5 (middle left chart below). If you don't have Excel 2013, you don't have this option, but you could
Manually type the desired text into the labels.
Link each label to the desired cell: select the labels (one click), select the desired label (second click), type = in the formula bar, select the cell, press Enter, repeat for all labels.
Use a third-party add-in like Rob Bovey's free Chart Labeler from appspro.com.
I hid the default axis labels. You can select None for axis labels, and then resize the plot area to make room for the custom labels created above. But I used a custom number format for the axis, which consisted of several space characters enclosed in double quotes. See middle right chart below.
Finally I rescaled the vertical axis so its min and max were 0 and 40 and the major spacing was 10, I formatted the XY series to use no markers, and I changed the label font color back to Automatic (bottom chart).

How to plot chart values outside axis maximum?

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

Given a fixed cel width, can I change the fontsize so that the text will fit inside the cell?

I have code that will automatically adjust the height of a cell, so that text will fit inside that cell.
However now I want to change the fontsize of the text inside the cell, so that the font will shrink and grow to make the text fit inside the cell, so I don't have to make the cell width a fixed size.
How do I do this in VBA?
I need to know the width of text, given a font size
Option 1.
TextWidth = WidthOfAText(Range("A1").Value)
FontAdjustmentFactor = Range("A1").EntireColumn.ColumnWidth / TextWidth
FontSize = Range("A1").Font.Size * FontAdjustmentFactor
Range("A1").Font.Size = Max(4, Min(10, FontSize))
or I need to know whether the text will fix and do some sort of trial and error routine.
Option 2.
Range("A1").Font.Size = 10
While (Text_does_not_fit AND Range("A1").Font.Size >= 5)
Range("A1").Font.Size = Range("A1").Font.Size - 1
Wend
How do I do this? (Preferably using code like in option 1)
Non programatically just right click on the cell -> Format Cells -> Alignment -> Shrink to Fit
Code wise is:
Sheet1.Range("A1").ShrinkToFit = True

Resources