I'm trying to generate area charts that comes out with green colors for values over a threshold, and red values for values under. I'm able to generate the charts, but my problem is that I don't get continuous graphs (they always come out with gap spaces between the different colored values, even if there is a number in the cell that represents the gap in the graph).
Sample code for the graphs:
Sub x_Graph()
'Define Chart
Dim c As Chart
Set c = ActiveWorkbook.Charts.Add
Set c = c.Location(Where:=xlLocationAsObject, Name:="RRL")
With c
.ChartType = xlArea
.HasTitle = True
.ChartTitle.Text = Sheets("Control Data").Range("C4").Value
End With
'Delete pre-defined series
Do Until ActiveChart.SeriesCollection.Count = 0
ActiveChart.SeriesCollection(1).Delete
Loop
With c
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = Sheets("PD").Range("AN$34:AY$34")
.SeriesCollection(1).Values = Sheets("PD").Range("AN$40:AY$40")
.SeriesCollection.NewSeries
.SeriesCollection(2).XValues = Sheets("PD").Range("AN$34:AY$34")
.SeriesCollection(2).Values = Sheets("PD").Range("AN$41:AY$41")
'Name series and format size
c.SeriesCollection(1).Name = "HU (days/month)"
c.SeriesCollection(2).Name = "LU (days/month)"
.Parent.Width = 700
.Parent.Height = 450
End With
c.SeriesCollection(1).Select
With Selection
.Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
End With
c.SeriesCollection(2).Select
With Selection
.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
End With
'Format fonts
c.Legend.Select
With Selection.Format.TextFrame2.TextRange.Font
.BaselineOffset = 0
.Size = 14
.Name = "Arial"
End With
c.Axes(xlValue).Select
With Selection.TickLabels.Font
.Size = 14
.Name = "Arial"
End With
c.Axes(xlCategory).Select
With Selection.TickLabels.Font
.Size = 14
.Name = "Arial"
End With
End Sub
The graphs come out like this:
With a gap in between the red and green area, even if there's data in the cell.
The input data is in the following format:
The green is created from the "Good" series and the red is created from "Bad"
And background code:
etc
Anyone know what could be done?
Many thanks in advance
FN
Related
I am having an additional data series included in a chart that I don't intend to include. It is only showing up on the first iteration and no others. The value of the series is "={1}". How did this data series come to be included in the chart and how can it be removed?
Set sh = Worksheets("LowDistCharts")
Set chrt = sh.ChartObjects.Add(0, 0, 300, 300)
Set ch = chrt.Chart
With chrt
.Height = 300
.Width = 300
.Top = 1 + ((aa - 4) * 300)
.Left = 1
End With
With ch
.HasTitle = True
.ChartTitle.Text = aa & " " & StartDate & " to " & EndDate
.ChartTitle.Font.Size = 8
.ChartType = xlLine
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = ActiveWorkbook.Worksheets("ActiveSheet").Range(RngXR, RngXR2)
'.SeriesCollection(1).AxisGroup = 1
.SeriesCollection.NewSeries
.SeriesCollection(2).Values = ActiveWorkbook.Worksheets("ActiveSheet").Range(RngStartR, RngEndR)
.SeriesCollection(2).AxisGroup = 2
.SeriesCollection(3).Delete
.HasLegend = False
End With
When adding a chart you may find one or more series "auto-added" if there are cells with data selected when the chart is created.
You can make sure these are removed before adding the data you want:
Set chrt = sh.ChartObjects.Add(0, 0, 300, 300)
Set ch = chrt.Chart
Do While ch.SeriesCollection.Count > 1
ch.SeriesCollection(1).Delete
Loop
To store the dates irrespective of the system format i made it string because the excel will be used by many users on different systems. Below is the code:
Range("Z:Z").NumberFormat = "#"
Range("AC:AC").NumberFormat = "#"
Range("AF:AF").NumberFormat = "#"
dateArr = Array("4/1/2016", "4/15/2016", "5/1/2016", "5/15/2016", "6/1/2016", "6/15/2016", "7/1/2016", "7/15/2016", "8/1/2016", "8/15/2016", "9/1/2016", "9/15/2016", "10/1/2016", "10/15/2016", "11/1/2016", "11/15/2016", "12/1/2016", "12/15/2016")
For i = 2 To UBound(dateArr)
ActiveSheet.Cells(i, 26).Value = Format(dateArr(i - 2), "yyyy/mm/dd")
Next
But i need to plot the scatter plot for it, so i again changed it to the date format as shown below:
Columns("Z:Z").Select
Selection.NumberFormat = "yyyy/mm/dd"
And when i plotted the scatter plot,its not plotting it properply.
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
ActiveChart.SetSourceData Source:=Range("Plot!$Z$2:$AA$" & date_no_row)
ActiveChart.Axes (xlCategory)
With ActiveChart.Axes(xlCategory)
.MinimumScale = 42401
.TickLabels.Orientation = 30
.TickLabels.NumberFormat = "yyyy/mm/dd"
.BaseUnitIsAuto = True
.MajorUnit = 14
.MinorUnitIsAuto = True
.Crosses = xlAutomatic
.ReversePlotOrder = False
End With
ActiveChart.Parent.Name = "Plot"
With ActiveChart.Parent
.Height = 325 ' resize
.Width = 900 ' resize
.Top = Range("C3").Top
.Left = Range("B2").Left
End With
ActiveSheet.ChartObjects("Plot").Chart.HasLegend = False
With ActiveSheet.ChartObjects("Plot").Chart
.HasTitle = True
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Dates"
.Axes(xlCategory, xlPrimary).AxisTitle.Font.Size = 15
End With
ActiveSheet.ChartObjects("Plot").Activate
ActiveChart.Axes(xlCategory).HasMajorGridlines = True
column AA contains numeric values. And the plot i m getting is as shown in the picture below
And expected plot is
You set column Z to text. After that, you try to set a number format, but the cells are text, so the number format never takes. Try it. Apply the first two code snippets and then manually change the cell format. It will have no effect.
So, start off with the correct format for column Z,
Range("Z:Z").NumberFormat = "yyyy/mm/dd"
I'm trying to change the color of the data points in an excel chart but everything im trying unsuccessful.
This is one method I tried, yet the points still appear blue:
With Chrt
.ChartType = xlXYScatter
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "=""Top Platen"""
.SeriesCollection(1).Values = yaxis
.SeriesCollection(1).XValues = xaxis
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
.Solid
End With
Here's another method I tried and still the data points appear blue:
With Chrt
.ChartType = xlXYScatter
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "=""Top Platen"""
.SeriesCollection(1).Values = yaxis
.SeriesCollection(1).XValues = xaxis
.SeriesCollection(1).Interior.Color = RGB(255,0,0)
This is only one segment of my code, I can supply additional areas if necessary. Any help would be greatly appreciated.
I believe the problem is with the nested With blocks getting confused. Here is one way to solve it and still use nested With block:
With Chrt
.ChartType = xlXYScatter
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
.SeriesCollection.NewSeries
With .SeriesCollection(1)
.Name = "=""Top Platen"""
.Values = yaxis
.XValues = xaxis
.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
End With
End With
Here is Microsoft's documentation link that talks about fully qualified nested With blocks.
I have been working on a macro for the past week to automatically create charts in excel. I have gotten pretty far along with it (thanks in large part to help from this website and its users), but I am stuck on a seemingly insignificant step. For some reason my line with markers graph shows up with discolorations in it. What I mean by this is that the middle fill of the marker is the standard blue that excel defaults to. I think the issue lies with the [ .Visible = msoTrue] line but no matter how I manipulate the code, I cannot make my markers one solid color.
The code is below
Sub DM1R_Graph()
Dim ws As Worksheet
For Each ws In Sheets
ws.Activate
If ws.Name <> "WSNs" Then
Dim sht As Worksheet
Dim xVals As Range, yVals As Range
Dim co As Shape, cht As Chart, s As Series
Set sht = ActiveSheet
Set co = sht.Shapes.AddChart()
Set cht = co.Chart
'remove any existing series
Do While cht.SeriesCollection.Count > 0
cht.SeriesCollection(1).Delete
Loop
cht.ChartType = xlLineMarkers
'get the extent of the XValues...
'below is the first Y axis entry (Oil)
'(change the 2nd offset number to get what you want)
Set xVals = sht.Range(sht.Range("B2"), sht.Cells(Rows.Count, "B").End(xlUp))
Set yVals = xVals.Offset(0, 2)
Set s = cht.SeriesCollection.NewSeries
s.XValues = xVals
s.Values = yVals
With s.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
.Solid
End With
With s.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
End With
'below is the second y axis entry (Gas)
Set xVals = sht.Range(sht.Range("B2"), sht.Cells(Rows.Count, "B").End(xlUp))
Set yVals = xVals.Offset(0, 4)
Set s = cht.SeriesCollection.NewSeries
s.XValues = xVals
s.Values = yVals
With s.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
.Solid
End With
With s.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
End With
'below is the third y axis entry (water)
Set xVals = sht.Range(sht.Range("B2"), sht.Cells(Rows.Count, "B").End(xlUp))
Set yVals = xVals.Offset(0, 5)
Set s = cht.SeriesCollection.NewSeries
s.XValues = xVals
s.Values = yVals
With s.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 240)
.Transparency = 0
.Solid
End With
With s.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 240)
.Transparency = 0
End With
'end Y axis entries
cht.HasLegend = True
'below applies the legend names to be whatever are in parenthesis'
cht.Legend.Select
ActiveChart.SeriesCollection(1).Name = "Oil (BO)"
ActiveChart.SeriesCollection(2).Name = "Gas (MCF)"
ActiveChart.SeriesCollection(3).Name = "Water (BW)"
'below applies the data labels
cht.SeriesCollection(1).Select
cht.SeriesCollection(1).ApplyDataLabels
cht.SeriesCollection(2).Select
cht.SeriesCollection(2).ApplyDataLabels
cht.SeriesCollection(3).Select
cht.SeriesCollection(3).ApplyDataLabels
'below orients the datalabels to either above,below,right,or left
cht.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).DataLabels.Select
Selection.Position = xlLabelPositionRight
cht.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).DataLabels.Select
Selection.Position = xlLabelPositionAbove
cht.SeriesCollection(3).Select
ActiveChart.SeriesCollection(3).DataLabels.Select
Selection.Position = xlLabelPositionLeft
'below moves the chart
Dim iChart As Long
Dim lTop As Double
lTop = ActiveSheet.Range("Q10").Top
For iChart = 1 To ActiveSheet.ChartObjects.Count
ActiveSheet.ChartObjects(iChart).Top = lTop
ActiveSheet.ChartObjects(iChart).Left = ActiveSheet.Range("Q1").Left
lTop = lTop + ActiveSheet.ChartObjects(iChart).Height + ActiveSheet.Range("5:7").Height
Next
'below deals with the chart title
cht.SetElement (msoElementChartTitleAboveChart)
With cht.ChartTitle
.Text = sht.Name & Chr(10) & "Oil,Gas, and Water Production Through Well Life "
.Characters.Font.Size = 12
End With
'below adds a filter to one column. You cannot have more than 1 filter per sheet.
Columns("L:L").Select
Selection.AutoFilter
End If
Next ws
End Sub
Below is a picture showing what I mean. You can see it obviously in the red series, but it also appears in the green and blue series as well.
I believe you need to set the MarkerBackgroundColor on the series.
s.MarkerBackgroundColor = RGB(255, 0, 0)
I used Excel VBA to create a chart and I want to adjust the line color and x-axis interval.
Below is the code:
Sub add_cpu_chart(server_hostname, site)
On Error Resume Next
Dim rpt_site As String
rpt_site = site
Set tbl = ThisWorkbook.Sheets(server_hostname).ListObjects(1)
strTableName = tbl.Name
strTableRange = tbl.Range.Address
Dim m_s_name, m_e_name As Date
m_s_name = CDate(fromDateStr)
m_e_name = CDate(toDateStr)
'Create new embedded chart
Set shp = ThisWorkbook.Sheets(server_hostname).Shapes.AddChart
'Position Shape over range
shp.Top = 100
shp.Left = 200
With shp
With .Chart
'Specify source data and orientation
'.SetSourceData Source:=ActiveSheets.Range(Table1 & "[#All]"), _
'PlotBy:=xlRows
.SetSourceData Source:=ThisWorkbook.Sheets(server_hostname).Range(strTableName & "[#All]")
.ChartType = xlLineMarkers
.SetElement (msoElementChartTitleAboveChart)
.HasTitle = True
.ChartTitle.Text = "CPU Utilization of " & server_hostname & " in " & _
rpt_site & " (" & Format(m_s_name, "dd-Mmm") & " to " & Format(m_e_name, "dd-Mmm yyyy") & ")"
.ChartTitle.Font.Size = 14
'.ChartArea.Font.Size = 14
.Legend.Delete
.SetElement (msoElementPrimaryValueAxisTitleRotated)
.Axes(xlValue, xlPrimary).AxisTitle.Text = "CPU Utlization (%)"
.Axes(xlValue, xlPrimary).AxisTitle.Font.Size = 10
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MinorUnit = 2
.Axes(xlValue).MaximumScale = 100
.Axes(xlValue).MajorUnit = 20
.Axes(xlValue).TickLabels.NumberFormat = "General"
.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Date of the Month"
.Axes(xlCategory, xlPrimary).AxisTitle.Font.Size = 10
'.Axes(xlCategory).MajorUnit = 1
.Axes(xlCategory).TickLabels.NumberFormat = "d"
With .PlotArea.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.150000006
.Transparency = 0
.Solid
End With
End With
End With
End Sub
I want to change the line color to red - as in excel 2003 - as well as change the data in interval separated by 2 days. Currently, the data is in the form 1,2,3,4... I want to display the data in the form 1,3 ,5....
In a line chart, the X axis' minimumscale, maximum scale, majorunit, and minorunit are meaningless. The X values are merely categories which have no interpreted numerical values even if the labels show numerals.
You should probably change to an XY chart, which will allow full control over the axis scale using minimumscale, maximum scale, majorunit, and minorunit. Of course, an XY chart allows the same formatting of series markers and lines as in a line chart, but the confusing terminology will live on forever.
If for some reason you decide to stick with a line chart, you can use
ActiveChart.Axes(xlCategory).TickLabelSpacing = 2