Gradient Colors changing unexpectedly - vba

I have been working on a formatting Macro that takes in selections and formats them according to chosen values in a userform. One of these options creates a large merged cell with a color gradient from a light to dark red.
When I run the macro, the cells are all formatted appropriately, but when I navigate around the sheet, I've noticed a strange issue where the color of the gradient will change to blue instead of red colors. Interestingly, the original color will randomly return, sometimes even giving a gradient from blue to red or red to blue.
If I use the format painter to pull the miscolored cell and test it elsewhere, the new broken/changing color scheme moves with it. I've included the code that formats the merged Cell below. Is there an issue, perhaps, with how I am designating the interior color?
With Range(WorkingRange(1, 2), WorkingRange(1, WorkingRange.Count - 1))
.Merge
.HorizontalAlignment = xlLeft
.WrapText = True
.Interior.Pattern = xlPatternLinearGradient
.Interior.Gradient.ColorStops.Add(0).Color = 255
.Interior.Gradient.ColorStops.Add(0).TintAndShade = 0
.Interior.Gradient.ColorStops.Add(1).Color = 130
.Font.Bold = True
End With

You are adding the ColorStop 0 two times. Second time with default color.
Should be
...
.Interior.Pattern = xlPatternLinearGradient
.Interior.Gradient.ColorStops.Clear
With .Interior.Gradient.ColorStops.Add(0)
.Color = RGB(255, 0, 0)
.TintAndShade = 0
End With
.Interior.Gradient.ColorStops.Add(1).Color = RGB(130, 0, 0)
...
Using the RGB function will make it more flexible for usage with different colors.

Related

Any codes that does clear color completely for powerpoint shape?

'I have tried to apply a color by vba with this code to a shape
.Fill.ForeCorlor.RGB = RGB(250, 0, 0)
'but soon I realize this color stay in memory* even though I tried to clear it with
.Fill.Transparency = 0.5 'Or
.Fill.Visible = msoFalse
'I do not want to use .Fill.ForeColor.TGB = RBB(255, 255, 255)
to set another color as it may overlay and block other content
*In memory: I mean when I applied a color to a shape and try to clear it with the above method, The next time I target shape with RGB(230,0,0), this shape that I thought has been previously set the color and clear still get targeted.
Appreciate for any help on this
G

How to check the background color of an excel cell

In my program, I set the background color of an excel cell using:
sheet.cells(row, column).interior.color = System.Drawing.Color.Red
In another part of my program, I want to see if the color is red, but this code:
If(sheet.cells(row, column).interior.color = System.Drawing.Color.Red) Then
'Do something
End If
It returns an 'Type cast invalid' exception.
If the color is checked by:
If(sheet.cells(row, column).style.interior.color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red))
'Do something
End If
The colors are said to be not equal (even though the cell is red), because the interior color is 16777215 and the color translator returns 255.
How can I compare the color the right way?
I'm not that sure, but System.Drawing.Color.xxx is not existing in vba. You could try this:
ActiveSheet.Cells(row, column).Interior.Color = RGB(255, 0, 0)
If (ActiveSheet.Cells(row, column).Interior.Color = RGB(255, 0, 0)) Then
'Do something
End If
You could also have a look to .Interior.ColorIndex
I think you should change the tag of question to vba instead vb.net.
Best regards.

Excel - Fill a cell with different colors

I need to fill a cell with different colors as in this picture (3 rows are merged vertically and colors are drawn manually in this picture using 3 rectangular shapes):
The only way I could find to fill part of a cell is using conditional formatting (by setting style as data bar and fill as solid) but it support only one color.
Is this possible with or without VBA?
It is possible.
I have found two ways to do that.
1- Using a black square shaped character (character code 2588 – vba: ActiveSheet.Cells(1, 1) = ChrW(&H2588)) and color them according to percentage. This character fills the cell height and also there is no spacing between them which allows filling a cell completely (Sure you should consider left indent in a cell). Only issue here that you cannot use a lot of characters in one cell; I use 30 of them and scale the number of characters according to 30 (ie. 50% red means 15 red character-2588).
2- It is same as what #Doktor Oswaldo has suggested: Inserting a plot in a cell using cell's position and size in pixels. This method has one big advantage: you can show the ratios exactly. In addition, you can fill a data series with a pattern as well. However if you have a lot of plots, you will sacrifice from Excel performance. For plot settings, I use following VBA code:
'Define var's
Dim src As Range, targetCell As Range
Dim chacha As ChartObject
'Set var's
Set src = Worksheets("Sheet1").Range("B1:B3")
Set targetCell = Worksheets("Sheet1").Range("C2")
'Create plot at the target cell
Set chacha = Sheets("Sheet1").ChartObjects.Add(targetCell.Left, targetCell.Top, targetCell.Width, targetCell.Height)
'Change plot settings to fill the cell
With chacha.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=src, PlotBy:=xlRows
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = 100
.Axes(xlCategory).Delete
.Axes(xlValue).Delete
.Legend.Delete
.PlotArea.Top = -50
.PlotArea.Left = -50
.PlotArea.Width = targetCell.Width
.PlotArea.Height = targetCell.Height
.ChartGroups(1).GapWidth = 0
End With
chacha.Chart.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
chacha.Chart.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(0, 0, 255)
chacha.Chart.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
In the code I modified the series colors manually which can also be automatized. Following is the screenshot of both methods. The Cell "C1" is filled with block characters and "C2" is a chart.
Note: You might get an error at the line ".PlotArea.Top". To solve this issue, please check: Error setting PlotArea.Width in Excel, VBA (Excel 2010)

VBA chart/object border/fill colour change

Hi I am currently working in Microsoft Excel 2010 and am trying to change the colour (and other properties) of a specific data point on a chart. So far I have tried:
ActiveChart.SeriesCollection(1).Points(4).Select
With Selection
.Interior.ColorIndex = 3
.MarkerSize = 10
.MarkerStyle = 8
End With
The code runs fine and the rest of the macro runs perfectly, (with size changing to 10 and style changing) apart from the colour staying the default blue! I have tried changing the colour index to Black and White - this doesn't seem to fix the issue.
After poking around I also tried:
ActiveChart.SeriesCollection(1).Points(4).Select
With Selection
.Interior.Color = RGB(255, 0, 0)
.MarkerSize = 10
.MarkerStyle = 8
End With
I also tried:
.ForeColor = RGB(255, 0, 0)
and
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
But I'm unsure how the last one works fully.
I'd really appreciate some help with this and I'd be happy to PM an example copy if someone would be willing to take a look!
(To reiterate - the activechart part of the function works fine - as does the following marker size and style changes)
-- Hash
You are trying to do this using properties that belong to cells
Try using this instead
With ActiveChart.SeriesCollection(1).Points(4)
.MarkerBackgroundColor = RGB(255, 0, 0)
.MarkerSize = 10
.MarkerStyle = 8
end with

Arrow To Indicate Special Points Using VBA

I have seen some examples which uses an Arrow to Indicate Special Points on Excel chart like this. But i want to achieve this using VBA. For example if some point on chart is greater then 90 then it shows an arrow corresponding to that point.
Please suggest on how should I go about it in VBA. Any help would be appreciated.
Update
Apart from just changing the point color is there any other good suggestion to make that point more prominent. Update 2
Right now i am using this code.
For Each oCell In Range("e4:e" & LastRow) 'loop
If oCell.Value < sd13 Then 'rule 13s
Range("V4").Value = "Rule 13s voilated!"
Range("V4:w4").Interior.Color = RGB(255, 0, 0)
ActiveWorkbook.Sheets("LDL-C").ChartObjects("Chart 1047").Chart.SeriesCollection(1).Points(j).MarkerBackgroundColor = RGB(255, 0, 0)
End If
Next
Apart from just changing the point color is there any other good suggestion to make that point more prominent.
Would this help?
With ActiveChart
For i = 1 To .SeriesCollection.Count
a = .SeriesCollection(i).Values
For l = 1 To .SeriesCollection(i).Points.Count
If mymax < a(l) Then
mymax = a(l)
.SeriesCollection(i).DataLabels.Select
Selection.Format.Line.Visible = msoTrue
Selection.Format.Line.Visible = msoFalse
.SeriesCollection(i).Points(l).DataLabel.Select
.SeriesCollection(i).Points(l).Select
.SeriesCollection(i).DataLabels.Select
.SeriesCollection(i).Points(l).DataLabel.Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 2
End With
End If
Next l
Next
End With
SNAPSHOT
Another snapshot
Not sure how to do it with an arrow but here is a way to just change the colour of the point of interest:
With ActiveSheet.ChartObjects(ChartName).Chart.SeriesCollection("NCDs")
For currentPoint = 1 To .Points.Count
If Range("G" & currentPoint + 34).Value = True Then
With .Points(currentPoint).Format
.Fill.BackColor.RGB = RGB(50, 150, 50)
.Fill.ForeColor.RGB = RGB(50, 150, 50)
.Line.ForeColor.RGB = RGB(50, 150, 50)
End With
Else
With .Points(currentPoint).Format
.Fill.BackColor.RGB = RGB(150, 50, 50)
.Fill.ForeColor.RGB = RGB(150, 50, 50)
.Line.ForeColor.RGB = RGB(150, 50, 50)
End With
End If
Next currentPoint
End With
Just change the names and the condition clause...
Also maybe the .Points(currentPoint) object has x,y location properties which you could use to position your arrow. Not sure about that though but it seems like a good starting point.
Yeah, gotta have VBA. Problem with VBA is that someone has to remember to run the procedure, or set up a Worksheet_Calculate event, or whatever, so when the data changes, which it inevitably does, the chart keeps up with the data.
So here's my non-VBA approach, which relies on Excel formulas.
Simple data, supplied by Siddharth in his answer. I've added a column, which I call MAX. The formula in cell C2, copied down to C11, is
=IF(B2=MAX(B$2:B$11),B2,NA())
The first chart plots the regular series of data in a clustered column chart. The second chart has MAX added to it. In the third chart I've changed the Overlap to 100%, so the blue bar covers the corresponding gray bar. Next chart I've added data labels to the MAX series. In the last chart I've formatted the data label to show series name, and the font color =matches the bar color.
So here is the original data and chart (upper) and changed data with changed chart (below). The formulas did it all, no need to somehow rerun the VBA.
What's cool is if I have a tie for first place, I get two labeled blue bars, with no extra effort.
Not a big stretch to add a third series to indicate the MIN.
ActiveChart.FullSeriesCollection(1).Select
With Selection
.MarkerStyle = 8
.MarkerSize = 5
End With
Selection.MarkerStyle = 2
ActiveChart.ChartArea.Select
With Selection.Format.Line