So I have a graph that has 6 series on it and I want to be able to make each series visible or invisible using a set of ActiveX check boxes. The check boxes seem to be working fine so far, but I can't figure out how to make an individual series invisible. Is there a property of the series that I can use to make it not visible? Google and Microsoft help aren't turning it up for me.
(Granted, that doesn't mean it isn't there...)
Any and all help is appreciated.
Update
Wait, I remember that I have come across this before in the past. A good way to hide the data you want is to hide the actual rows / columns of data being charted. So your ActiveX control won't touch the chart, but rather hide / unhide the columns / rows of chart data.
You will just need to set up your chart data in such a way that it doesn't interfere with any other data that needs to be shown.
Leaving my original answer for anyone's benefit...
I just recorded a macro and played with it ...
Got this great result:
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Select
'Turn it on
Selection.Format.Line.Visible = msoTrue
'Turn it off
Selection.Format.Line.Visible = msoFalse
This is eqiuvalent to selecting No Line in the Line Color section of the Format Data Series dialog box.
When I get stumped in Excel, I always try recording a macro, a la WWBGD? (What Would Bill Gates Do?)
Here's the result for a bar chart where I set the fill to "None":
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Select
Selection.Format.Fill.Visible = msoFalse
Does that help?
The above 2 did not work for me, in case of ChartType = xlLineMarkers.
What did work was:
For Each mySeries In .SeriesCollection
With mySeries
.Border.LineStyle = xlNone
.MarkerStyle = xlNone
.MarkerBackgroundColorIndex = xlColorIndexNone
End With
Next
However, the legend does still display the line. It is necessary to delete that entry separately.
In Excel 2013, to make a serie invisible, taking it also out of the legend etc, use the filter option, .
Vba Example:
The Following code Hides / Restores the series by filtering it Out/In of the chart.
ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).IsFiltered = True / False
This is equivalent to manually filtering a series in/out using the Ribbon command Design + Select Data, (which is available when the char is selected).
All series options, like dashes colors, markers etc.. are restored unchanged.
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(linenumber).Select
Selection.Format.Fill.Visible = msoFalse
This works great, select the chart than i have 24 lines, with a listview with checkboxes and with 24 entries. Click on 1 and it disapears, click again and its back. You alsow see the line dis- and appear in the legend.
Related
I am in the process of creating the following chart:
But I need each data label to be centered over the line that gets created instead of the end point of the line. I have the following:
Set mypts = mysrs.Points
mypts(mypts.Count).ApplyDataLabels
With mypts(mypts.Count).DataLabel
.ShowSeriesName = True
.ShowCategoryName = False
.ShowValue = False
' optional parameters
.Orientation = 0
.Position = xlLabelPositionAbove
.Font.Size = 10
.Font.Bold = True
End With
Does any body know how to center the data labels, which I gave it by giving each series a name?
Centering a label above each line will involve creating another data series that calculates the line's center. Without having access to your data, I turned to #JonPeltier 's excellent post on connecting two XY data series.
Your question involves your custom VBA code. My example here should be taken as the steps needed to set up the data. At your choice you can either set up your data on the worksheet, or have your VBA code create this "extra" data series to add to the chart (again, because I don't know what your data looks like I can't recommend a solution specific to your problem).
I won't repeat all the details of creating the chart, but here's an example of the data and the resulting chart:
Now, setting up the mid-point data isn't difficult. The mid-point is a quick formula =((D2-A2)/2)+A2, copied down the column.
Added to the chart, select the "MidPoint" and "Value" columns and cntl+c copy the data, then select the chart and "Paste Special" as a new data series.
A Quick Note: DO NOT put your mid-point data on the same rows as the A and B data. I did this initially and Excel (in its own private wisdom) associated the data with column A. The results on the chart aren't pretty.
Next to last step is to select the new data series and select "Add Data Labels", then check the box for "Value From Cells". Choose the data range for the labels, e.g. cells C17:C20.
Finally, format the labels to only show the "Value From Cells" (uncheck the "Y" data label) and set the marker to "None". Then you have the centered label for your line.
I have code that loops through sheets of data and creates charts based on each set of data. That works great. But when I added a line to the code to add a black border to each chart (j is the index used to loop through and identify the appropriate chart):
With ActiveSheet.Shapes("MyChart" & j).Line
.ForeColor.ObjectThemeColor = msoThemeColorText1
End With
Excel displays the following message box for each chart being created when I run the code:
"Complex formatting that is applied to the selected chart may take a while to display. Do you want to continue using the formatting?
Yes/No"
The code works fine if I just keep clicking "Yes" for each chart it's creating until the code is finished, but I don't want Excel to ask this at all, I want it to just go ahead with the formatting.
Everywhere I've looked in my research for how to do this says to set:
Application.DisplayAlerts = False
But this isn't working. Excel still displays the Yes/No box every time. Are there other ways to suppress messages/alerts in Excel? Why isn't the above line of code working?
As shown in the image below, I have a chart (on the left) that I created manually. And I have the chart on the right which I created with the following VB Script:
Sub StackedBarChart()
'
' StackedBarChart Macro
'
'
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.PlotBy = xlColumns
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SeriesCollection("Total").Format.Fill.Visible = msoFalse
End Sub
Where I am falling short with my macro is the following areas:
I need to set just the "Total" data label to InsideBase
I need to rescale the y-axis. But, this needs to work for any data set. So, for example, taking the highest total value and adding $2.5 to it in order to make it a decent looking chart.
Automatically make sure that all of the Data Labels on the Legend appear. Right now, only 4-12 appear.
Thanks for your help!
With the
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
you select the whole line including the totals. So on top of your stacked chart you have the total values again, so this doubles the high.
With (and here I am not quite sure)
ActiveChart.SeriesCollection("Total").Format.Fill.Visible = msoFalse
you just supress the display of the 'Total' stack. Just try to comment this line out, and I supose that you will get another element on top.
The best you probably can do is to redo the chart manually while recording a macro and checking the statements there.
For the legend you possibly need to increase the size of the display area.
The totals you can possibly dray an invisible stack with a 100% overly in the background. Then the values should be shown but no bar is displayed.
I want to change bar color of a chart based on Quarter-Quarter grouping in an exported excel file automatically, with the least manual work. I tried VBA to do that, but blocked by not knowing how to get X-axis title.
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Select
'Check bar caption
'If bar captioin = 'FY13 Q1' then do following...
ActiveChart.SeriesCollection(1).Points(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid
End With
My question:
Is VBA the best way to do that?
Is there any better way? Can some excel built-in function already support to do it?
If I want to automate the whole process as much as possible, how to integrate macro to the exported excel file without creating macro-->copying-->pasting.
I would use VBA yes, but then that's just what I work with
The property you're looking for is
ActiveSheet.Charts("Chart 1").Axes(xlCategory).AxisTitle.Text
To make sure the macro is available in all worksheets, create an addin file and ask future users to install it. You didn't mention what version of Excel you're using, but the Excel 2010 reference on Addins is http://msdn.microsoft.com/en-us/library/office/gg597509%28v=office.14%29.aspx
EDIT: rereading your post I think you may have meant to ask about Data Labels rather than the X-Axis Title. In which case you'll want to use:
ActiveSheet.Charts("Chart 1").SeriesCollection(1).Points(1).DataLabel.Text
Now with this and the above, you'll want to make sure the Title/Data Label exists first, for example with :
If ActiveSheet.Charts("Chart 1").SeriesCollection(1).Points(1).HasDataLabel Then...
If you need help with this, or with looping through the charts /points, it's probably best to look for the answer first and ask a separate question if you can't find.
Cheers!
I'll try to be very precise with my problem but if you have any questions, please ask!
So basicelly I have a column A full of dates (from line 1 to line 80) and a column B full of values (stock prices, and also with the same range).
I have created a chart with this code :
Sub Chart()
Range(Cells(1, 1), Cells(80, 1))Select
Range(Selection, Selection.Offset(0, 1)).Select
Set rSource=selection
ActiveChart.SetSourceData Source:=rSource, PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
end sub
So this is not a chart object but a new chart on a new sheet (I say that because all the things I've read on this subject is dealing with chart objects and not with chart alone in a sheet).
Now what I need is to draw an horizontal line between two dates (a support line).
For example, I need to draw a line between 01/01/2013 (line 1) and 06/25/2013 (line 80).
The line has to stay at the same level on the y-axis (that's what we call an horizontal line :) ) at the level of the stock price of 01/01/2013.
I've tried that :
Sub supportline()
Dim CoordA As Variant
CoordA = [a1:b80]
ActiveChart.Shapes.AddLine(CoordA(1, 1), CoordA(1, 2), CoordA(80, 1), CoordA(80, 2)).Select
end sub
But the line isn't drawn where I want. I think there's a problem of scale but I can't figure out how to have the accurate coordinates.
Thank you very much for your time and your help
EDIT (06/11/2013)
I've simplified the data base to illustrate my problem : http://cjoint.com/?CFksHluf0VD I'm trying to make a line between the two dates (01/01/2013 and 16/01/2013)
If you download this file, you'll see in the VBA part : 1/ Test1 : I've tried to make a line to link 2 points of the chart : if you launch the sub you'll see that the line is drawn but at the wrong scale and wrong place 2/ Test 2: I've tried to make an horizontal line between two points but that doesn't work at all
Test 2 is what I need to do for my original project (an horizontal line which goes from one date to another with the same y-value)
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
//add line style etc
End with
Then use your select statement to choose which points you want joined.
If you look at this link, I'll think you'll find it most useful
VBA-Controlled Conditional Formatting of Line Chart Lines
http://peltiertech.com/WordPress/conditional-formatting-of-lines-in-an-excel-line-chart-using-vba/
Another question on Stack Overflow
Excel VBA - How do you set line style for chart series?
In response to your comments, please see the answer to this question. It offers some good explanation of using charts (in this case vb.net)
Chart: Show more value descriptions on X-Axis
And a link to MSDN re this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart.aspx