Control Shape Gradient Properties via VBA - excel

I'm trying to find a means of controlling the Gradient fill properties of a 'shape' in Excel 2016. I have 8 shapes (called Shape1-8) which will act as colour swatches for the gradient of a MASTER shape. The positioning of each swatch (gradient stop) will (eventually) be controlled via either spinners / scrollbars.
Couldn't find much online soo I turned to the good old 'Record Macro' whilst I tried to reverse engineer the functionality. This is were the problems began...
So I constructed a simple 2 colour gradient, Black to White.
Then added a third colour, Red.
Moved the Red stop from default 50% to 83%
Then added a fourth colour, yellow
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.BackColor.RGB = RGB(250, 250, 250)
.TwoColorGradient msoGradientHorizontal, 1
End With
Selection.ShapeRange.Fill.Visible = msoTrue 'Added Red
Selection.ShapeRange.Fill.Visible = msoTrue 'Moved Red from 50% to 83%
Selection.ShapeRange.Fill.Visible = msoTrue 'Added Yellow
I was hoping to find a means to pull existing colour from Swatch Shape (1-8) to update the associated Gradient Stop (1-8) in the MASTER, like so....
...Shape("Master").Gradient1.backcolor.fill = ...Shape("Swatch1").Backcolor.fill
without having to code the RGB passing from the swatch controls
...Shape("Swatch1").BackColor.RGB = RGB(TextRed.value, TextGreen.value, TextBlue.value)
into
...Shape("Master").Gradient1.BackColor.RGB = RGB(TextRed.value, TextGreen.value, TextBlue.value)

Related

VBA: Change Font & Back colour of ActiveX Textbox

I have a TextBox just on the worksheet , I barely have tried everything to change Font and Back colours of it , but seems like can change only if TextBox is part of UserForm
Will be very appreciate for answers
To change the font color, let's say to red, try . . .
Worksheets("Sheet1").OLEObjects("TextBox1").Object.ForeColor = RGB(255, 0, 0)
To change the back color, let's say to blue, try . . .
Worksheets("Sheet1").OLEObjects("TextBox1").Object.BackColor = RGB(0, 112, 192)
Change the worksheet name and the RGB colors accordingly.

Series.Format.Fill.Transparency property not applying formatting

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

How to change the color of a bullet in a bullet list?

I am trying to change the colour of a bullet point using VBA, but keep getting stuck. I want to make all the bullet points in a bullet list blue instead of black.
In Word you can not simply set the ParagraphFormat.Bullet.Font.Color (as in PowerPoint), but assign another style, e. g.
Selection.ParagraphFormat.Style = "List with red buttons"
Microsoft explains you alter its ParagraphFormat, using code similar to:
With Application.ActivePresentation.Slides(1).Shapes(2).TextFrame
With .TextRange.ParagraphFormat.Bullet
.Visible = True
.RelativeSize = 1.25
.Font.Color = RGB(255, 0, 255)
End With
End With
And, here's a color picker so you may get just the precise shade desired, translated into the RGB format (xxx,yyy,zzz) required.

Define Shadow for SeriesCollection in Chart

I have an issue with setting the shadow to series collections in a xlXYScatter chart. The shadow is drawn but with ignoring all the parameters (color, transparency etc.). I think I do not call the shadow property correctly for the series collection. But I don't know how to do it right. Any suggestions?
Thanks!
...
With .SeriesCollection(1).Format.Shadow
.ForeColor.RGB = RGB(0, 0, 0)
.OffsetX = 3
.OffsetY = 3
.Transparency = 0.4
.Visible = True
End With
Alright, I found a solution by myself. Just for someone having the same issue. With using:
...
.SeriesCollection(1).Format.Shadow.Type = msoShadow21
...
the shadow is drawn to the series collection. The different types of shadow can be set by changing the number e.g. msoShadow34 or msoShadow25 etc.

Change backcolor of shape

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.

Resources