I try to find a solution for setting the color in a user-forms bar chart (chchart) transparent. The approach for regular bar charts doesn't work:
With ActiveChart.SeriesCollection(6)
.Format.Fill.ForeColor.RGB = rgbBlue ' <-- "Format"property doesn't exist in "chchart"
.Format.Fill.Transparency = 0.9
End With
It looks like there is no transparency property for a chchart. Is there a workaround?
Thanks.
Related
I am trying to resize an excel chart with VBA.
Is it possible to change ActiveChart.ChartArea.Height without affecting the size of the PlotArea? Whenever I attempt to change the chart height it looks like the plot area is automatically resized, which is an undesired outcome.
I have tried the following sequence, when downsizing a graph:
Changing plotarea to fixed desired height;
Changing chart height to fixed desired height;
Changing plotarea to fixed desired height;
This sequence does not yield expected results, as (1) the chart is not changed to specified height, and (2) the plotarea height is output correctly, but its positioning within the chart (.InsideTop) has changed.
Please, test the next way of dealing with a chart dimensions. The scenario involves the next process: firstly memorizing the PlotArea dimensions (Height/Width), then play with the chart (Object) dimensions, reset the PlotArea ones and set its Position to Automatic. Excel tries guessing what you want accomplishing and it looks/is more probable that both chart elements to be modified proportionally:
Sub testActiveChartDimensions()
Dim ch As Chart, plHeight As Double, plWidth As Double
Set ch = ActiveChart 'plays with a selectded chart
plHeight = ch.PlotArea.height: plWidth = ch.PlotArea.width 'memorize the plot area dimensions
ch.Parent.height = ch.Parent.height * 2: ch.Parent.width = ch.Parent.width * 2 'resize the chartObject
ch.PlotArea.height = plHeight: ch.PlotArea.width = plWidth 'reset the initial dimensions for plot area
' you can set any other dimensions (just to be lower than the new chart dimensions...)
ch.PlotArea.Position = xlChartElementPositionAutomatic 'center it on the chart object
End Sub
I have a chart in which I create a number of new series for using the Chart.SeriesCollection.NewSeries method, there after I immediately format each series. All formatting seems to work (marker size/style/colour) bar marker transparency which I set via the Series.Format.Fill.Transparency property.
I have seen people using the selection method to set series marker transparency as per below:
Set GraphSeries.Absolute.ErraticErrors = AbsChart.SeriesCollection.NewSeries
With GraphSeries.Absolute.ErraticErrors
.Name = "ErraticSeries"
.MarkerSize = CommonMarkerSize
.MarkerBackgroundColorIndex = Graph.XY.Formatting.MarkerFillColour.ErraticErrors
.MarkerForegroundColor = Graph.XY.Formatting.MarkerLineColur.ErraticErrors
.Select
Selection.Format.Fill.Transparency = CommonMarkerTrans
.Format.Line.Visible = msoFalse
End With
After trying and troubleshooting the above method, it seems when the marker transparency is set, the marker background colour can be seen to change value rather than the transparency which remains at its initial value, strange? Based on this finding, I reverted back to Series.Format.Fill.Transparency = CommonMarkerTrans and viewed this property in the watch window while setting it to CommonMarkerTransand observed no change to the initial value of Series.Format.Fill.Transparency (-2.147484E+09), also strange?
Seems that the Series.Format.Fill.Transparency property is not changing from its intial value when trying to set it to CommonMarkerTransvia Series.Format.Fill.Transparency = CommonMarkerTrans
Note: CommonMarkerTrans= 0.9 as single
Anyone have any ideas why this is? I'm really stumped here, could it be related to the ordering in which I am setting the marker formatting?
Below shows a snippet of code to provide context. The code creates an 'empty' chart series and then applies various formatting, such as marker size, and then sets the series X and Y values from data stored in two separate arrays.
Set GraphSeries.Absolute.ErraticErrors = AbsChart.SeriesCollection.NewSeries
With GraphSeries.Absolute.ErraticErrors
.Name = "ErraticSeries"
.MarkerSize = CommonMarkerSize
.Format.Fill.Transparency = CommonMarkerTrans
.MarkerBackgroundColorIndex = Graph.XY.Formatting.MarkerFillColour.ErraticErrors
.MarkerForegroundColor = Graph.XY.Formatting.MarkerLineColur.ErraticErrors
.Format.Line.Visible = msoFalse
End With
RelOrAbsChart.SeriesCollection(SeriesName.ErraticSeries).XValues = XAxisRelOrAbs.ErraticSeries
RelOrAbsChart.SeriesCollection(SeriesName.ErraticSeries).Values = YAxisRelOrAbs.ErraticSeries
Seems I may have solved this. For some reason you must use the following properties to set for/background colour of series markers if you want to use Fromat.Fill. Transparency to set the marker Transparency
Series.Format.Fill.ForeColor.RGB = RGB(R,G,B)
Series.Format.Fill.BackColor.RGB = RGB(R,G,B)
And not
Series.MarkerBackgroundColor = RGB(R,G,B)
Series.MarkerForegroundColor = RGB(R,G,B)
Not sure why this is the case, seems confusing to have two separate properties to set Marker for/background colour and not be able to set transparency for both.
Why would you need two separate and mutually exclusive properties for the same thing? For clarity, maybe someone could clear this up or correct my understanding?
Functioning code which now sets colour and transparency correctly:
Set GraphSeries.Absolute.ErraticErrors = AbsChart.SeriesCollection.NewSeries
With GraphSeries.Absolute.ErraticErrors
.Name = "ErraticSeries"
.MarkerSize = CommonMarkerSize
.MarkerStyle = xlMarkerStyleCircle
With .Format.Fill
.Visible = msoTrue
.ForeColor.RGB = Graph.XY.Formatting.MarkerFillColour.ErraticErrors
.BackColor.RGB = Graph.XY.Formatting.MarkerLineColur.ErraticErrors
.Transparency = CommonMarkerTrans
.Visible = msoTrue
End With
End with
I am trying to set the color of even lines and markers according to the color of odd lines and colors.
Let's say, my table looks like this (speed1, performance1, speed2, performance2,.....)
When I plot a line chart It just gives every line a different color. In my case I want to give every two columns (speed and performance) a single color. So, for that I try every time to take the color of a line and the color of its markers, and then assign these colors to the following line (color of odd columns to even columns).
This is my code:
Sub ChangeColors()
Dim cht As Chart
Dim ser As Series
Set cht = ActiveChart
For i = 1 To cht.SeriesCollection.Count
If (i Mod 2) = 0 Then
Set dst = cht.SeriesCollection(i)
Set src = cht.SeriesCollection(i - 1)
dst.Format.Line.Visible = msoFalse
dst.Format.Line.Visible = msoTrue
dst.Format.Line.ForeColor.RGB = src.Format.Line.ForeColor.RGB
dst.Format.Fill.ForeColor.RGB = src.Format.Fill.ForeColor.RGB
End If
Next i
End Sub
The problem is that at the end all the even lines become black. However, when I try to pick a RGB color (for instance, RGB(255, 0, 0)) an assign it to the even lines they all become red, so it works.
UPDATE:
When I use a MsgBox to print the value of src.Format.Line.ForeColor.RGB and src.Format.Fill.ForeColor.RGB I get 16777215 and 0 respectively for all lines. So I don't understand why these values and why they are the same for all lines ?
Does anyone know what is the problem with my code ?
Thank you !
Should this line:
dst.Format.Line.ForeColor.RGB = src.Format.Fill.ForeColor.RGB
instead be:
dst.Format.Line.ForeColor.RGB = src.Format.Line.ForeColor.RGB
I have an Excel chart which changes on selections made in slicers.
I noticed that the plot area and the legend area change depending on the made selection.
I tried to fix the position and size for the plot area using vba, but this simply does not work unfortunately.
The plot area and the legend keep on resizing, causing the legend overlapping the plot area. Which I obviously do not want.
I have this code, placed in the worksheet page of the vba editor:
Option Explicit
Private Sub Chart_Calculate()
ChartObjects("grafiek 4").Activate
ActiveChart.PlotArea.Width = 637.783
ActiveChart.Legend.Left = 716.514
ActiveChart.Legend.Width = 176.735
ActiveChart.Legend.Height = 295.334
End Sub
having this code, I assumed the automatic resizing would be gone, but I saw the legend sometimes still overlaps te plot area.
Is there a solution which permanently fixes this problem?
Edit1:
Yesterday, I simply added a few parameters for the plot area. It seemed to work then. But now I tried again, and the legend is overlapping the plot area again.
I changed the code to:
Option Explicit
Private Sub Chart_Calculate()
ChartObjects("grafiek 4").Activate
ActiveChart.PlotArea.Top = 33.102
ActiveChart.PlotArea.Left = 67.1
ActiveChart.PlotArea.Width = 637.783
ActiveChart.Legend.Top = 7
ActiveChart.Legend.Left = 716.514
ActiveChart.Legend.Width = 176.735
ActiveChart.Legend.Height = 329.667
End Sub
So with 2 more paramters for the plot area.
edit2:
I have checked the legend properties in Excel. under 'options for legend' there is a checkbox: show legend without overlapping plot area (I do not know the exact english text).
This box is checked, but it does overlap the plot area.
Why is it impossible to achieve this? Having fixed sizes for the plot area and the legend should not be so hard.
edit 3:
I do have this routine now in my workbook:
Option Explicit
Private Sub Chart_Calculate()
ChartObjects("grafiek 4").Activate
With ActiveChart
With .PlotArea
.Top = 33.102
.Left = 67.1
.Width = 637.783
End With
With .Legend
.IncludeInLayout = True
.Position = xlLegendPositionRight
.AutoScaleFont = False
.Font.Size = 8
.Top = 5
.Left = 706.899
.Width = 179.735
.Height = 336.681
End With
End With
End Sub
Sub kopieergrafiek()
ActiveSheet.ChartObjects("Grafiek 4").Copy
End Sub
(including the suggestion in the comment below my post)
I does not seem to work. Does worksheet_change event perhaps works better?
edit 4:
I still do not have a solution for this issue. It already happens when the name of 1 of the legend items is to long to fit the space. And it also happens when there are to many items in the legend to fit in the space available.
I think there is no solution for this. Unless I could somehow tell excel to maximize the number of items in the legend. or to maximize the length of the series name.
I was having this problem myself with the legend resizing the plot area. I tried what Portland Runner suggested, only setting .Legend.IncludeInLayout to false (thus separating the legend from the plot area as he suggested, perhaps he made a typo?) and my plot area was no longer resized.
I also had this problem and found this answer. I found a fix that works for me. Not sure why exactly it works but I do these steps:
Set the legend position
Undock the legend (legend.includeLayout = false)
Resize the plot area to the size I wanted
Re-dock the Legend (legend.includeLayout = True)
Set the Legend.Left position
and after that the legend is correctly positioned and lined up.
I know this is an old thread, but since I had the same issue and was able to solve it, but did not really find the correct answer on this thread, I thought it would be good to post my solution. It is really important to refresh the pivotlayout, otherwise you will not see a difference in the Chart Legend. This will adjust the Legend so it will not overlap the plot area. It will also increase the size of the chart, so if you do not want that, you will need to use other code.
Sub AdjustChartLegendActiveSheet()
Dim j
For j = 1 To ActiveSheet.Shapes.count
If ActiveSheet.Shapes(j).Type = msoChart Then
ActiveSheet.Shapes(j).chart.Legend.IncludeInLayout = True
ActiveSheet.Shapes(j).chart.PivotLayout.PivotTable.PivotCache.Refresh
End If
Next j
End Sub
I'm trying to update the backcolor of all of the labels that I have on a worksheet. I'd like to indicate the color using the RGB values, but I'm stuck in two places. Here is the code that I have right now:
Sheet2.Shapes("Label 2").Fill.BackColor.RGB = RGB(220, 105, 0)
This code will run without error, but it seems to have no effect. My label starts out white (or maybe transparent) and never changes. Can anyone tell me what I need to do to make this work? I also added this but it did nothing:
shp.Fill.Solid
Next, I'd like to capture this RGB value in a variable so that I don't have to re-type of repeatedly. Essentially, I'm looking for something like this:
dim col as Color
col = RGB(220,105,0)
Sheet2.Shapes("Label 2").Fill.BackColor.RGB = col
I know that there is no variable type called Color, but I think you can see what I am trying to do.
Try setting the ForeColor instead:
Sheet2.Shapes("Label 2").Fill.ForeColor.RGB = RGB(220, 105, 0)
A good way to figure out what to do is to record a macro while you make the adjustment yourself. You can examine the generated code afterward and use it as a starting point.
Here is an example of a procedure that will add a rectangle to the activesheet, add some text to it, and then color it with your RGB values:
Public Sub AddRectangleWithText()
Dim textRectangle As Shape
Set textRectangle = ActiveSheet.Shapes & _
.AddShape(msoShapeRectangle, 10, 80, 250, 50)
' add your text
textRectangle.TextFrame.Characters.Text = "Your mother was a hamster."
' fill the shape with the rgb color of your choice
textRectangle.Fill.ForeColor.RGB = RGB(220, 105, 0)
End Sub
ForeColor actually controls the backcolor of comment/textbox(s) in Excel as follows;
Activecell.Comment.Shape.Fill.ForeColor.RGB = RGB(240, 255, 250)
'Mint Green
as the Comment/TextBox ForeColor is a foreground fill over the Applicatoin background color.
I needed to add
textRectangle.Fill.Visible = msoTrue
in Office 2016 for
textRectangle.Fill.ForeColor.RGB = RGB(220, 105, 0)
to have an effect.