chart series data in first iteration of for loop vba - vba

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

Related

Create multiple column stacked graphs with two series

I want to create 40 different column stacked graphs using macros in Excel. Each graph has two series. So far I've done this:
Sub loopChart()
Dim mychart As Chart
Dim myRange As Range
Dim c As Integer
Dim d As Integer
c = 1
d = 2
While c <= 33 '1=dataSource1, 4=dataSource2, 7=dataSource3
While d <= 33
'set data source for the next chart
With Worksheets("Hoja1")
Set myRange = .Range(.Cells(1, c), .Cells(3, c + 1))
Set myRange2 = .Range(.Cells(1, d), .Cells(3, d + 1))
End With
'create chart
Sheets("Hoja1").Select
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.ChartType = xl3DColumnStacked
'sets source data for graph including labels
.SetSourceData Source:=myRange2, PlotBy:=xlColumns
'including legend
.SetElement (msoElementLegendRight)
.HasTitle = True
'dimentions & location:
'defines the coordinates of the top of the chart
.Parent.Top = 244
'defines the coordinates for the left side of the chart
.Parent.Left = c * 100
.Parent.Height = 200
.Parent.Width = 300
.ChartTitle.Text = "Porcenta-2014"
End With
c = c + 3
d = d + 3
Wend
Wend
End Sub
I want that insted of 1 and 2 my graphs use de name of the column A:
Sub tset()
Dim dataRange As Range
Dim dataNames As Range
Dim c As Integer
Set dataRange = Range("B1:C3")
Set dataNames = Range("A2:A3")
c = 1
For i = 1 To 40
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.SetSourceData Source:=dataRange, PlotBy:=xlColumns
.SeriesCollection(1).XValues = dataNames
.ChartType = xl3DColumnStacked
.SetElement (msoElementLegendRight)
.HasTitle = True
.Parent.Top = 244
.Parent.Left = c * 100
.Parent.Height = 200
.Parent.Width = 300
.ChartTitle.Text = "Porcenta-2014"
End With
c = c + 3
Set dataRange = dataRange.Offset(0, 2)
Next i
End Sub

create several charts in Excel with VBA

I have a worksheet with 300 columns and would like to create one scatter plot for each column, bringing data from two other sheets that are in the same worksheet.
The problem is that I´m not familiar with VBA, and some error codes don't help at all.
Private Sub Create_Charts()
Dim sh As Worksheet
Dim chrt As Chart
For i = 1 To 300
Set sh = ActiveWorkbook.Worksheets("Graphs")
Set chrt = sh.Shapes.AddChart.Chart
With chrt
'Data
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "=""Scatter Chart"""
'With the following parameters it works:
'.SeriesCollection(1).XValues = "=OP!$c$4:$c$1588"
'.SeriesCollection(1).Values = "=PV!$c$4:$c$1588"
'But I need something like this:
.SeriesCollection(1).XValues = CStr(Worksheets("PV").Range(Cells(i, 4), Cells(i, 1588)))
.SeriesCollection(1).Values = CStr(Worksheets("OV").Range(Cells(i, 4), Cells(i, 1588)))
'Location
.ChartArea.Left = 380 * i - 380
.ChartArea.Top = 100
.ChartArea.Height = 360
.ChartArea.Width = 360
'Formatting
.Axes(xlCategory).HasMajorGridlines = True
.Axes(xlValue).HasMajorGridlines = True
.HasAxis(xlCategory, xlPrimary) = False
.HasAxis(xlValue, xlPrimary) = False
.HasLegend = False
End With
Next i
End Sub
This line references a column:
.SeriesCollection(1).XValues = "=OP!$c$4:$c$1588"
This references a row, row i from column 4 to column 1588:
.SeriesCollection(1).XValues = CStr(Worksheets("PV").Range(Cells(i, 4), Cells(i, 1588)))
But you also need to reference not only Range but Cells. And if you pass in a string address, you need the leading "="; it's easier to pass in a range. So try this:
Dim wsPV As Worksheet, wsOV As Worksheet
Dim rngX As Range, rngY As Range
Set wsPV = ActiveWorkbook.Worksheets("PV")
Set wsOV = ActiveWorkbook.Worksheets("OV")
Set rngX = wsPV.Range(wsPV.Cells(4, i), wsPV.Cells(1588, i)
Set rngY = wsOV.Range(wsOV.Cells(4, i), wsOV.Cells(1588, i)
chrt.SeriesCollection(1).XValues = rngX
chrt.SeriesCollection(1).Values = rngY

part of area chart not filled vba

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

VBA chart position changes when rows inserted

I wrote the following code to add a chart and position it on a worksheet with data on it:
Dim sh As Worksheet
Dim chrteit As Chart
lastrows = Range("A2").End(xlDown).Row
Set sh = ActiveWorkbook.Worksheets("TraceTable")
Set chrteit = sh.Shapes.AddChart.Chart
With chrteit
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = sh.Range(Cells(2, 6), Cells(lastrows, 6))
.SeriesCollection(1).Values = sh.Range(Cells(2, 7), Cells(lastrows, 7))
.HasTitle = True
.ChartTitle.Text = "EIT"
.Parent.Height = Range("N2:N14").Height
.Parent.Width = Range("N2:T2").Width
.Parent.top = Range("N2").top
.Parent.Left = Range("N2").Left
End With
The problem is, later in my module I have a macro that will an entire row between two data points if the two data points are different, and it is as follows:
Private Sub Dividers()
Dim DividerRange As Range, lastrow As Long, k As Integer, counter As Integer
lastrow = Range("C2").End(xlDown).Row
Set DividerRange = Range(Cells(2, 3), Cells(lastrow, 3))
counter = 0
For k = 2 To DividerRange.Count
If DividerRange(k + counter).Value <> DividerRange(k + counter - 1).Value Then
DividerRange(k + counter).EntireRow.Insert
counter = counter + 1
Else
End If
Next k
End Sub
By adding the entire row, it changes the height of my graph and it's position. I want it to be a fixed position, how can I do this? I would PREFER not to change the second code, but rather the first but let me know any solutions you guys have, Thanks!
Add this line to the first procedure:
chrteit.Placement = xlFreeFloating
This is the same as right-click, format chart area, properties: Don't move or size with cells.
|
Or you could place that method inside the With block, thusly:
With chrteit
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = sh.Range(Cells(2, 6), Cells(lastrows, 6))
.SeriesCollection(1).Values = sh.Range(Cells(2, 7), Cells(lastrows, 7))
.HasTitle = True
.ChartTitle.Text = "EIT"
.Parent.Height = Range("N2:N14").Height
.Parent.Width = Range("N2:T2").Width
.Parent.top = Range("N2").top
.Parent.Left = Range("N2").Left
.Placement = xlFreeFloating
End With

Excel VBA - Adjust Color Line and X-Axis Interval

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