Changing bar colors in bar graph - excel

I've created a VBA for Excel 2007 program that automatically creates bar graphs for ROI based on up to 52 different tabs in the active workbook. I'm close to done, and the only thing I cannot figure out is how to change the colors of the bargraphs.
The graphs are created in their own subfunction, called with a call like so. Every variable changes around whenever it's called.
Call AddChartObject(1, 1, "Example", extraWeeks, weekDifference)
My sub that it calls looks like this.
Sub AddChartObject(j As Integer, k As Integer, passedChartTitle As String, xtraWks As Integer, ttlWks As Integer)
Dim topOfChart As Integer
topOfChart = 25 + (350 * j)
'Adds bar chart for total sales
With ActiveSheet.ChartObjects.Add(Left:=375, Width:=475, Top:=topOfChart, Height:=325)
.Chart.SetSourceData Source:=Sheets("Consolidation").Range("$A$" & 3 + ((17 + xtraWks) _
* j) & ":$C$" & (4 + ttlWks) + ((17 + xtraWks) * k))
.Chart.ChartType = xl3DColumnClustered
.Chart.SetElement (msoElementDataLabelShow)
.Chart.HasTitle = True
.Chart.ChartTitle.Text = passedChartTitle & " Sales"
.Chart.SetElement (msoElementLegendBottom)
.Chart.SetElement (msoElementDataLabelNone)
.Chart.RightAngleAxes = True
End With
End Sub
The RGB color I want to use on the SECOND series in the bar chart is (155, 187, 89), per marketing's wishes. I'm pretty sure there is a .chart.????.???? = RGB (155, 187, 89) command I can use in my With to set this, but I have spent far too much time trying to figure it out, only to come up with nothing.

Have you tried
.Chart.SeriesCollection([index]).Interior.Color = RGB(155, 187, 89)
(where [index] is a placeholder for the series you want to change the color for)?

It works for me ScottyStyles in a very similar situation, but only for the first series collection. I used the same right below that, and that was not changing the color of the SeriesCollection(2). That one is a linear set of datas.
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ClearToMatchStyle
ActiveChart.SeriesCollection(1).Interior.Color = RGB(85, 142, 213)
ActiveChart.SeriesCollection(2).Interior.Color = RGB(192, 0, 0)

to change different bars inside a collection you can use:
ActiveChart.SeriesCollection(1).Points(1).Format.Fill.ForeColor.RGB = RGB(85, 142, 213)
ActiveChart.SeriesCollection(1).Points(2).Format.Fill.ForeColor.RGB = RGB(192,0, 0)
...

Related

Graphs won't overlap eachother

I have X amount of graphs where X is always an even number. Every series shows up as two graphs. One line graph and a Xy Scatter plot. The problem is that the graphs are separated, one to the left, one to the right. I want them to overlap so the points from the Scatter plot shows where the lines break. This is what I want:
But this is what I get:
By the way, these are two different graphs. One shows dates, the other weeks. The one with dates work, the week one doesn't.
This is the code concerning the week graph:
With myChart.Chart
.SeriesCollection(i).XValues = actualdateweek
'actualdateweek is an array with week numbers like 21,22 and so on
.SeriesCollection.NewSeries
If LineGraph = True Then
.SeriesCollection(i).Values = actualdata
.SeriesCollection(i).ChartType = xlLine
.SeriesCollection(i).Format.Line.Visible = msoTrue
.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(ColorR(i - iModifier - 1), ColorG(i - iModifier - 1), ColorB(i - iModifier - 1))
'actualdata is an array with all the data
elseif XyScatterPlot = True Then
.SeriesCollection(i).Values = actualdata
.SeriesCollection(i).ChartType = xlXYScatter
.SeriesCollection(i).MarkerStyle = xlMarkerStyleSquare
.SeriesCollection(i).Format.Fill.Visible = msoTrue
.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(ColorR(i - iModifier - 1), ColorG(i - iModifier - 1), ColorB(i - iModifier - 1))
.SeriesCollection(i).MarkerForegroundColorIndex = -4142
End If
.SeriesCollection(i).AxisGroup = 1
Next i
End With
The graphs are added in turns. First a data series (the actualdata array) is added as a line graph. Then the same data series is added but as a scatter plot. Then the data series changes and it all repeats. So it's like
seriescollection(1).XValues = array filled with week numbers
seriescollection(1).Values = Data series 1
seriescollection(1).ChartType = xlLine
seriescollection(2).XValues = array filled with week numbers
seriescollection(2).Values = Data series 1
seriescollection(2).ChartType = xlXYScatter
seriescollection(3).XValues = array filled with week numbers
seriescollection(3).Values = Data series 2
seriescollection(3).ChartType = xlLine
seriescollection(4).XValues = array filled with week numbers
seriescollection(4).Values = Data series 2
seriescollection(4).ChartType = xlXYScatter
This is what the first graph looks like:
Then when the data to the second graph is added:
.SeriesCollection(i).Values = actualdata
Then the second graph is changed into a scatter plot:
.SeriesCollection(i).ChartType = xlXYScatter
Then an axis group is assigned:
.SeriesCollection(i).AxisGroup = 1
Anyone have an idea what is happening?
edit
Ok problem solved I guess...
By removing the X value for the XYScatterPlot it aligned with the Line Graph.
This line:
.SeriesCollection(i).XValues = actualdateweek

Draw Shape Based on Cell Values

All,
I have code that creates a shape based on based on inputted values in the macro itself. I am wanting to have the values of shape type, width, and height specified by the user (location of shape on the sheet is indifferent to me right now). The user would input the aforementioned numerical values for width and height into the cells and click a button which would output the shape type and size the user wants.
In my case, there will be a drop down box for "rectangle" and "circle". I don't know how to get the code to read those words and convert it '1' and '9', respectively. I may just have the user choose 1 or 9 to create the shape.
I would also like to add text to the center of the shape. Again, I have created a code for this but it is within the macro. I would like to have the code reference a cell value instead. I assume it would be the same as above.
Thank you for any assistance.
Sub AddShape()
Dim s As Shape
Dim ws As Worksheet
Set ws = Sheets("Deck Layout")
'add a shape
Set s = ws.Shapes.AddShape(1, 80, 80, 75, 75)
'make it nearly white
s.Fill.ForeColor.RGB = RGB(245, 245, 255)
'show text within it
s.TextFrame.Characters.Text = "1"
s.TextFrame.Characters.Font.ColorIndex = 2
With s.TextFrame.Characters(0, 0)
s.TextFrame.HorizontalAlignment = xlHAlignCenter
s.TextFrame.VerticalAlignment = xlVAlignCenter
.Font.Color = RGB(0, 0, 0)
End With
End Sub
Since you've already got parts of the answer in the comments, I'll focus on the shape picking.
Have a look at this:
Dim ShapeType As MsoAutoShapeType
Select Case LCase(ws.Range("b1").Value)
Case "rectangle"
ShapeType = msoShapeRectangle
Case "circle"
ShapeType = msoShapeOval
End Select
Set s = ws.Shapes.AddShape(ShapeType, 80, 80, 75, 75)
It will find the value in B1, convert it to lower case and the test it for "rectangle" and "circle" and the set the ShapeType to a corresponding value.
You can use 1 and 9 instead, but that is bad practice. Use the defined constants - it will make your code much easier to read.

Set min/max value of the X and Y axes in an excel chart through vb.net

As the Title suggest, I need to find the way to set the maximum and minimum value for the X and Y axes of a chart created through a vb.net code. This is the part of the code it should be inserted in:
With ws
.Shapes.AddChart(Excel.XlChartType.xlXYScatterSmoothNoMarkers, 300, 20, 400, 300).Select()
With xlApp.ActiveChart
.SeriesCollection(1).Name = "Force vs Displacement"
.SeriesCollection(1).XValues = "='Sheet" & index & "'!$B$6:$B$14046"
.SeriesCollection(1).Values = "='Sheet" & index & "'!$C$6:$C$14046"
.HasLegend = False
[...]
Thanks for any suggestion made.
Sorry everybody. I finally found the correct page to look at, now almost everything is clear. The code for me is:
xlApp.ActiveChart.Axes(2, 1).maximumscale = 1.2
xlApp.ActiveChart.Axes(2, 1).minimumscale = -0.2
where in brackets are XlAxisType and XlAxisGroup. 2 is for xlValue, whereas the 1 is for xlPrimary. Here you can find all I needed to not post my question:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.axes?view=excel-pia
Sorry once more. See ya!

How do I change the thickness of the lines in an Excel chart?

I managed to create a chart in an Excel worksheet with the following code:
let chartobjects = chartSheet.ChartObjects() :?> ChartObjects
let chartobject = chartobjects.Add(400.0, 20.0, 550.0, 350.0)
chartobject.Chart.ChartWizard(Title = "P&L and Benchmark",
Source = chartSheet.Range("A1", "C" + (string (slen))),
Gallery = XlChartType.xlLine, PlotBy = XlRowCol.xlColumns,
SeriesLabels = 1, CategoryLabels = 1,
CategoryTitle = "", ValueTitle = "NAV")
chartobject.Chart.ChartStyle <- 5
The series plotted have many data points (about 6,000) and fluctuate (stock price data). For this reason the chart lines appear very thick. I would like to make them thinner. This can be done interactively in Excel. Is there a way to do this from F#?

Smartart hierarchy nodes - can only fill in one textframe of each node

I am trying to build an organization chart automatically from data in Excel using Excel VBA. It works out fine, however, I would like to have both textframes filled in. In the big textframe I would like to have filled in the description of the department, and in the smaller textframe I would have like to add the department code.
smartart hierarchy layout
I can't find the code to access the smaller textframe.
Do While Source.Cells(Line, 1) <> ""
If Source.Cells(Line, 3) = PID Then
Set ParNode = QNode
If Source.Cells(Line, 4) = 1 Then
Set QNode = QNode.AddNode(msoSmartArtNodeDefault, msoSmartArtNodeTypeAssistant)
Else: Set QNode = QNode.AddNode(msoSmartArtNodeBelow)
End If
QNode.TextFrame2.TextRange.Text = Cells(Line, 6)
'here something needs to be added !!!
CurPid = Source.Cells(Line, 2)
If Not Found Then Found = True 'something was find
'Source.Rows(Line).Delete
'Line = Line + 1
Call AddChildNodes(QNode, Source, CurPid)
Debug.Print ("CurPid" & CurPid)
Debug.Print ("line" & Line)
Set QNode = ParNode
'ElseIf Found Then 'it's sorted,so nothing else can be found
' Exit Do
'Else
End If
Line = Line + 1
Loop
the upper line (where your CEO-text is)
QNode.TextFrame2.TextRange.Text
***.SmartArt.AllNodes(...).Shapes(1).TextFrame2.TextRange.Text
the lower line where your smartart is empty:
***.SmartArt.AllNodes(...).Shapes(2).TextFrame2.TextRange.Text
you need to check if QNode.Shapes(2).TextFrame2.TextRange.Text works. if not, you may need to use .parent

Resources