I am using VBA with Excel 2013. I am developing a Macro with a chart, inside the chart there is a line made with a serie if points. I need apply a transparency shape to the steps between 2 point.
example
1 to 2 solid
2 to 3 transparent
3 to 4 solid
With .transparency = 0 and 1 the shape is applied to the whole line. I tryed to apply .trasnparency various time but it is applied to the whole line.
How do I apply transparency to have the behauvior described above?
The easiest way to get a gap is to insert a blank row between segments you want to be visible. In the top view below the data range is continuous, and so is the chart. In the bottom view, I've inserted blank rows between the X-Y values for the horizontal segments, and the chart rewards me with gaps in the plot, so the verticals don't show up.
You can't get these gaps if your code is inserting arrays into the chart series source data, but you can use code like this to make them disappear (by using No Line instead of Transparency, personal preference):
Sub TransparentVerticals()
Dim srs As Series
Dim iPt As Long
Set srs = ActiveChart.SeriesCollection(1)
For iPt = 1 To srs.Points.Count Step 2
With srs.Points(iPt)
.Format.Line.Visible = msoFalse
End With
Next
End Sub
Related
I have a fairly simple VBA code which is doing 75% of what I need it to do, I just can't get the last step to work properly.
In an Excel sheet, I have two sets of data (X data and Y data). The x data is the same two data values for every single case. Then I have two columns of data which comprise the Y data. I need to create an Excel scatter plot with a whole bunch of data series. Series 1 is the two constant x values with the first y value from the first column and the first y value from the second column. The 2nd series is the same two constant x values with the second y value from the first column and the second y value from the second column, and so on. The columns have about 300+ rows (hence > 300 series total). I realize that a scatter plot can only do up to about 256 series in one plot so I won't be able to fit them all in one plot. That's not the point. I've tried just plotting like, say, 50 of the series and I have that part working fine. Next, because all the series are straight lines, I need the slope/regression information from each series, so I create a linear trendline and plot the equation on the chart. I have this part automated and working correctly in Excel as well. Here's where I'm stuck though. The last step is, after I've had VBA create the, say, 50 series in the single plot and generate all of the regression equations, I then need for Excel to take those equations and paste them into a column in the main worksheet (where the plotting data resides). I found something on Google that is supposed to do this (grabbing trendline.datalabel.text and pasting it into the sheet), but it isn't working right, and I can't figure out what I'm doing wrong. I've tried 20 different things and am still beating my head against a wall. Any help with the last half of the code would be greatly appreciated. THANK YOU.
Sub Plot_slopes()
Dim i As Integer
Dim ChtOb As ChartObject
Dim objTrendline As Trendline
Dim strEquation As String
Set ChtOb = ActiveSheet.ChartObjects.Add(Left:=20, Width:=800, Top:=20, Height:=250)
ChtOb.Chart.ChartType = xlXYScatterSmoothNoMarkers
ChtOb.Activate
i = 9
For i = 9 To 59
With ActiveChart.SeriesCollection.NewSeries
.Name = "FS" & i
.XValues = Worksheets("summary").Range(Worksheets("summary").Cells(3, 7), Worksheets("summary").Cells(4, 7))
.Values = Worksheets("summary").Range(Worksheets("summary").Cells(i, 13), Worksheets("summary").Cells(i, 14))
.Trendlines.Add
End With
With ActiveSheet.ChartObjects(1).Chart
Set objTrendline = .Trendlines(1)
With objTrendline
.DisplayRSquared = False
Trendlines(1).DisplayEquation = True
strEquation = .DataLabel.Text
Range("Q9").Offset(i, 0) = strEquation
End With
End With
Next i
End Sub
I mainly need help with the code after the first With Block and where it starts with a new "with"..."with activesheet.chartobjects(1).chart", etc), although, I'm also having trouble with the .name part in the first With block as well. Goal is to get the last block so that VBA writes out the regression equations for each created series into the main "summary" worksheet
I create a pie chart in Excel 2013 using VBA. Everything works like expected: The chart is painted and each segment of that chart has its percentage value attached to it.
Now I have the problem that I got a lot of parts that are below 1% of the data making that chart very ugly with all that "0%" parts and its labels.
Now I still want all pies (otherwise I would just have filtered the source data) but I do only want lables on segments that are at least 2% of the data.
Is that possible?
Set DataSource = CreatePivotTableCurrFy
If Not (DataSource Is Nothing) Then
' Create chart object
Call ThisWorkbook.Worksheets("META").Shapes.AddChart(xlPie, 600, 200, 504, 360)
Set Co = ThisWorkbook.Worksheets("META").ChartObjects(2)
Co.chart.SetSourceData Source:=DataSource
Co.chart.ChartTitle.Text = "Sales by Brand"
Co.chart.SeriesCollection(1).ApplyDataLabels ShowPercentage:=True, ShowValue:=False
End If
You can try this not really very neat solution.
Dim d As Datalabel, Dim v As Long
For Each d In Co.chart.SeriesCollection(1).DataLabels
'v = CLng(Mid(d.Caption, 1, Len(d.Caption) - 1))
v = CLng(Split(d.Caption, "%")(0)) '~~> just thought this is better
If v < 2 Then d.Delete
Next
it is possible, just a bit complicated :) I take you want to hide the portion under 2% and you need to it for the slice as well as the legend and so on.
Naturally you start by selecting the slices of the pie that doesn't reach 2% (that is quite easy and it depends on how you give that % to your slices).
Then you can look here for a full procedure to follow. This link shows some code to do what I explained.
I'm fairly new to VBA and I'm trying to implement a certain type of graph based off of the following example data: https://docs.google.com/spreadsheet/pub?key=0AjZu7FPYRXsjdEJMLTN2MTZhUldpNnhYeW0wNF8taFE&output=html
I've attempted using the macro recorder but I haven't had any luck getting it to work as required.
I'd like to have a ScatterPlot with lines and Markers with the X-Values of the chart equal to the dates presented, the left Y axis be a range from 0 to 4 (with the steps inbetween equal to 1) and I'd also like to have a second y-axis where the max is the max number presented in the data (so in this case 45). Also, is there any way to name the series other than 'Series1', 'Series2' etc.?
In the end I'll cut down the number of series (probably a max of six).
Anyways any help would be greatly appreciated!
Edit: Here is the code provided by the macro recorder:
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
ActiveChart.SetSourceData Source:=Range("TimelineGenerator!$C$1:$W$11")
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MaximumScale = 4
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MajorUnit = 1
But when I execute this to generate my graph via vba, it has the dates as series instead of the xValues of the chart.
How can I add a custom horizontal line that has a label and it is at the exact same level as the first column in the chart (see the screenshot below).
Can this be done in VBA?
This could be done in VBA, or it could be done without VBA:
http://peltiertech.com/Excel/Charts/AddLineHorzSeries.html
This method involves creating a secondary Y-axis, and plotting another series of data in a "line" on the second axis.
This is a fairly clean solution.
Otherwise with VBA you would need add a shape/line to the chart (important to add it to the chartObject and not to the Worksheet).
Then compute the height of points and make the line's .Left = the chart's .PlotArea.Left and make the line's .Width = to the chart's .PlotArea.Width. Then set the line's .Top value based on the chart's .PlotArea.Height minus the "height" you calculated for the point.
using vba, you can add a new series:
With ActiveChart.SeriesCollection.NewSeries
.Values = "={6.9,6.9,6.9,6.9}"
'create string beforehand if number and values are unknown
.ChartType = xlLine
'and whatever other formatting is needed
End With
not using VBA, you can add a new column to the data, and put all of it equal to the first item, using =$B$2 in each cell to add the line to the graph
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