Changing Bar colors using VBA based on category label - excel

I have a VBA code in excel to change colors of bar graph but its not working for category series.
ActiveChart.SeriesCollection(1).Interior.Color = RGB(0, 153, 64)
I want to change the color of a single bar. However, the above code changes the color of all bars.
For all bars I want one color (green) except for two bars representing two data points (Average1 and average2); these should be of a different color. Can anyone please tell me how to to this with VBA?

Jesse's answer is often the cleanest way to do it.
However, it is not accurate that "to have different colored bars they must be on different series" (my emphasis). You can mix and match colors within one series. For example, this makes the second bar of the first series red:
ActiveChart.SeriesCollection(1).Points(2).Interior.Color = RGB(255, 0, 0)
You can use this to do all kinds of neat tricks, such as highlighting bars that exceed some threshold, are associated with leap years, or whatever. You could certainly choose to highlight your average1 and average2 values this way.
If you want to change the color for a point that has a given characteristic, then you have to loop through all points until you find a point that has that characteristic. For example, if you want to color in red the point whose category (XValue) is "avg" then you could do this:
Dim c As Chart
Dim s As Series
Dim iPoint As Long
Dim nPoint As Long
Set c = ActiveChart
Set s = c.SeriesCollection(1)
nPoint = s.Points.Count
For iPoint = 1 To nPoint
If s.XValues(iPoint) = "avg" Then
s.Points(iPoint).Interior.Color = RGB(255, 0, 0)
End If
Next iPoint

Your problem isn't with the VBA, to have different colored bars they must be on different series.
Leave a gap in the base colored series and add the values you want colored on a second series and color that. Your data would look something like this:
Series | Month 1 | Month 2 | Month 3 | Month 4 | Month 5 | Month 6 | Month 7
1 10 12 15 14 10
2 17
3 18

Related

How Can I create a distribution chart in Microsoft Excel?

I want to create a histogram in Microsoft Excel. I think this should be easy but I am oddly having some difficulties.
I have the following table:
Bins | Frequency
50000 | 800
100000 | 500
150000 | 300
and so on. The Bins column shows the bin width for this histogram. The Frequency column shows the number of values in each bin. How can I then turn the above table into a histogram?
Since you already have the Bins and Frequency created, you can select these cells and insert a normal column chart under Insert -> Charts -> 2D-Column.
In order to make it look more like a histogram you can alter the formatting of the chart.
First change the Gap Width under Series Options -> Series Options to 0%
Then add in a border to help distinguish the bars under Series Options -> Fill & Line
You can also play around with the Axis names and add "Frequency" to the y-axis and "Bins" to the x-axis by clicking on the Chart Elements Button (Green plus when hovering over chart) and selecting Axis Titles.

Changing the color of agents in NetLogo according to a turtle-own variable

I am writing a simple food exchange model in netlogo and I want the agents to change their color as their [food] level changes in the model. The amount of food is in range [0,1] and I want the color to change from white to red (white = food level of zero and red = food level of 1) with the code below:
ask turtles [
set color scale-color red food 1 0 ]
But my turtles turn black somehow in the middle of food exchange! Turtles own food value can be any floating point number in the range [0,1]. Does anyone know how I can keep the color within the light shades of red (red to white) and no black?
Scale-color and ranges
From the example above, the color and number are correct, but the issue seems to be with the range provided. Since food is within [0,1], the color gradient should match the changes, though it will be from 0 (white) to 1 (black).
As JenB mentioned, you might want to extend the range of the expected values. Changing the range from [0,1] to [0,2] for scale-color would help, since with scale-color the midpoint of the range is the color provided.
[ set color scale-color red food 2 0 ]
As long as food is within [0,1], this example should fluctuate between red and white.

Add horizontal axis per series in excel

How frustrating is Excel.. working on this for half an hour now.
I simply try to make a frequency plot of two groups, with different colours. On the x-axis I would like to display the subject.ids per bar.
However, if I select a different range for the horizontal x axis per series (series 1 = blue, series 2 = orange) with the subject id, it changes the x-axis in the other series to the same. What in hell am i doing wrong?
3007 1
23121 1
3009 1
3005 1
3011 2
23171 2
3207 2
3102 3
3207 6
13302 7
2411 11
23191 11
3008 11
3106 12
110031 1
110031 1
110030 1
110017 1
110014 1
110008 1
110004 1
110007 2
110035 4
110020 4
110003 4
110036 10
110019 11
110015 21
AFAIK, you cannot put 2 series onto the x axis.
You have 2 alternate ways to solve your problem:
Concatenate each positional pair into a new column and use this as the x-axis label series. It will look like this:
You could use data labels for each series. However, this will add the data to the columns themselves and not the axis (you could put it at the base of the column). To do so, you will need to right click on the graph, select 'Add Data Labels'. By default it adds the value as the label, but you can select the labels, right click to format the data labels and use the 'values from cells' option. Once you do this and play around with the orientation and location of the labels, it will look like this:
For simplicity, I'd go with the first method
Adding a 3rd option; simply put the columns for the axis labels beside each other and when selecting the Data for the Axis Labels, just select both columns instead of the usual 1. It will look like this:

Excel: Format Color of A Group of Rows Based on the Maximum Value of these Rows

I have the following problem. Given the data:
City Begin
1 2000-01-01
1 2002-02-01
1 2002-02-01
2 2002-02-01
2 2003-02-01
2 2005-12-01
3 2002-04-01
3 2003-07-01
3 2005-12-01
I wanted to group rows by the city they belong to. For each group, I wanted to color all rows in that group red if the maximum date in that group does not equal 2005-12-01, and blue if otherwise. In the sample data, this means that all rows with city 1 is colored red, and those with cities 2 and 3 are blue.
Is there a way to achieve this? Thanks!
Non-VBA method:
Conditional formatting -> New Rule...
Select "Format all cells based on thier values"
Format Style = "2-Color Scale"
Colors = [As needed]
VBA method:
Dim rng As Range
Set rng = ActiveSheet.Range("A1:A10") 'or where-ever.
With rng.FormatConditions
.AddColorScale 2
With .Item(1).ColorScaleCriteria(1)
.Type = xlConditionValueLowestValue
.FormatColor.Color = 15773696
End With
With .Item(1).ColorScaleCriteria(2)
.Type = xlConditionValueHighestValue
.FormatColor.Color = 255
End With
End With
EDIT: It's late, so after reading the question again I may have got the colors reversed... Emphasis on "As needed".

excel:changing the symbol and color of a marker based on grouping

I have a dataset that looks like
ID Vehicle_grp count mpg
000 Car 5 10
Motorbike 20 100
Other 1 25
001 Car 30 60
Motorbike 28 45
Other 85 35
002 Car 100 10
Motorbike 20 200
Other 1 65
etc.
In excel, how do i change the colors and marker symbols used based on the ID and vehicle group. I would like to change colors based on ID, and use a different symbol for each vehicle group (i.e car is circle, motorbike is triangle, and other is cross)
I don't know VBA, so if theres a way to do this through the menus, that would be appreciated
Just make sure you add each series separately. Here are the steps (assume Excel 2007 or greater):
Insert > Scatter
Click the chart > Design > Select Data
Add first series. Series Name = the cell containing 000. Series X Values = cells for count 5, 20, and 1. Series Y values = cells for mpg 10, 100 25.
Add the second series. Series Name = cell containing 001. Series X Values = cells for count 30, 28, 85. Series Y values = cells for mpg 60, 45, 35.
Add the third series according to above.
Following that process gives me:
A little clean-up and formatting, gives me:

Resources