I am generating a chart from table. the table rows are not completely filled.
so when I am generating an chart, I get the value of not filled columns also as "0". I would like to remove the "0" (value of blank rows") in my chart. Is
there a way I can do this? any lead would be helpful
Sub chartResult()
Dim rng As Range
Dim cht As Object
Set rng = ActiveSheet.Range("B2:D53")
ThisWorkbook.Sheets("Result").ChartObjects.delete
Set sh = ActiveSheet.ChartObjects.Add(Left:=440, _
Width:=600, _
Top:=340, _
Height:=250)
sh.Select
Set cht = ActiveChart
With cht
.SetSourceData Source:=rng
.ChartType = xlColumnClustered
End With
cht.SeriesCollection(1).name = "Ov"
cht.SeriesCollection(2).name = "O"
cht.SeriesCollection(3).name = "To"
cht.SeriesCollection(1).HasDataLabels = True
cht.SeriesCollection(2).HasDataLabels = True
cht.SeriesCollection(3).HasDataLabels = True
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(72, 118, 255) '<~~ Red
cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
cht.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 167, 0)
cht.HasTitle = True
cht.ChartTitle.Text = "Result "
End Sub
Try the property Chart.DisplayBlanksAs and set xlNotPlotted.
So in the code the change looks some what as shown below in with statement.
With cht
.SetSourceData Source:=rng
.ChartType = xlColumnClustered
.DisplayBlanksAs = xlNotPlotted //ADD THIS STAEMENT.
End With
#Jenny - I can't guarantee this will work, I don't work with charts, but give it a try.
With cht
.SetSourceData Source:=rng
.ChartType = xlColumnClustered
.PivotItems("0").Visible = False
End With
Related
I have a sheet and I am generating the Chart from a table.
I would like to generate 2 charts. One chart with absolute numbers and other chart for the same data with Percentage.
Right now, for this, I am using two code, just by adding a line for generating the chart with Y.axis in percentage.
I would like to define column where my chart will start (for eg: chart1 from G7) and chart2 from G15. (I don't have this in my code)
I also, would like to define the length , height and width for my chart.(I don't have this in my code)
It would be great if you can help me add this requirement and do it In a single program.
Sub chartstatus()
Dim rng As Range
Dim cht As Object
Set rng = ActiveSheet.Range("A2:E53")
Set sh = ActiveSheet.Shapes.AddChart
sh.Select
Set cht = ActiveChart
With cht
.SetSourceData Source:=rng
.ChartType = xlColumnClustered
cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
End With
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 255) '<~~ Red
cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
cht.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
cht.HasTitle = True
cht.ChartTitle.Text = "Result 2017"
End Sub
I use the same code, deleting the line to generate the second chart
cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
The easier way if to use the ChartObject to create and define the chart, and then modify all it's properties (such as position and dimension).
The code below will create the first chart, place it in Cell "G7", and I modifed it's dimensions to show you the properties you need to modify.
You can add another one for the second chart (with an easy copy>>paste).
Code
Option Explicit
Sub chartstatus()
Dim Rng As Range
Dim ChtObj As ChartObject
Set rng = ActiveSheet.Range("A2:E53")
' use ChartObject instead of shape
Set ChtObj = ActiveSheet.ChartObjects.Add(100, 100, 500, 500) '<-- default dimension and location >> can modify later
With ChtObj
.Chart.ChartType = xlColumnClustered
.Chart.SetSourceData Rng
With .Chart
.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 255) '<~~ Red
.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
.HasTitle = True
.ChartTitle.Text = "Result 2017"
End With
' set position of the chart to Cell G7
.Top = Range("G7").Top
.Left = Range("G7").Left
' change the dimensions of the chart
With .Chart.ChartArea
.Width = 1060
.Height = 420
End With
End With
End Sub
For changing the chart location :
vba to add a shape at a specific cell location in Excel
For chart size:
sh.Width =100
sh.Height =100
I have a sheet with Pivot table and I am creating an Column stacked chart from the table.
I would like to change the colours of Legend entries in chart and would like to have an title for the chart every time I create the chart.
I tried the below code and I'm getting
Object variable or with block variable not set
I get the error in the line
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(155, 213, 91)
Could anyone, tell me how I could create chart for my requirements. I have added an image of my current chart and required chart.
Sub chart11()
Dim sh As Shape
Dim cht As Chart
If ActiveSheet.PivotTables.Count = 0 Then Exit Sub
Set ptable = ActiveSheet.PivotTables(1)
Set ptr = ptable.TableRange1
Set sh = ActiveSheet.Shapes.AddChart
sh.Select
With ActiveChart
.SetSourceData ptr
.ChartType = xlColumnStacked
End With
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(155, 213, 91)
cht.Axes(xlCategory).MinimumScale = 5
cht.Axes(xlCategory).MaximumScale = 40
cht.HasTitle = True
cht.ChartTitle.Text = "Default Chart"
End Sub
This is the chart generated by my code now, if I comment the error line:
I would like to have a chart of this type with change in colour legends and chart title:
You can't use cht.Axes(xlCategory).MinimumScale = 5 and MaximumScale.
Because Pivot chart do not assist Scatterchart.
Only in scatter chart, you can set xlCategory Scale.
Sub chart11()
Dim sh As Shape
Dim cht As Chart
If ActiveSheet.PivotTables.Count = 0 Then Exit Sub
Set ptable = ActiveSheet.PivotTables(1)
Set ptr = ptable.TableRange1
Set sh = ActiveSheet.Shapes.AddChart
sh.Select
Set cht = ActiveChart
With cht
.SetSourceData ptr
.ChartType = xlColumnStacked
End With
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0) '<~~ Red
cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(0, 255, 0) '<~~ green
'cht.Axes(xlCategory).MinimumScale = 5
'cht.Axes(xlCategory).MaximumScale = 40
cht.HasTitle = True
cht.ChartTitle.Text = "Default Chart"
End Sub
I tried to find the solution on the internet, but none of them worked for my situation.
I have the following chart and I want to hide it, but I don't know how:
Dim cht As Chart
Sub CreatePie()
arrColors = Array(RGB(183, 212, 117), _
RGB(0, 93, 172))
Set cht = Sheets("Dashboard").Shapes.AddChart(Left:=600, Width:=160, Top:=290, Height:=90).Chart
With cht
.SetSourceData Source:=Sheets("Data").Range("M5:N6")
.ChartType = xlPie
.ChartArea.Format.Fill.Solid
.ChartArea.Format.Fill.Transparency = 1
.ChartArea.Border.LineStyle = xlNone
End With
With cht.SeriesCollection(1)
.Points(1).Format.Fill.ForeColor.RGB = arrColors(0)
.Points(2).Format.Fill.ForeColor.RGB = arrColors(1)
End With
cht.Visible = xlSheetVeryHidden
End Sub
cht.Visible = False doesn't work either.
Deleting it is also an option, but cht.Delete doesn't work either.
You need to modify the Parent (the ChartObject) in order to hide the whole chart. So instead of your line :
cht.Visible = xlSheetVeryHidden
modify to :
cht.Parent.Visible = False
I am trying to write code that will format a chart (myChart). Series line 2 and 4 are the 2 that I need to format so their weight = 1. I have tried following with no luck.
myChart.fullseriescollection(2).format.line.weigth = 1
I am pretty new to VBA, and I have never tried to reference chart object before, so I am unfamiliar with the proper syntax.
Thanks! See my code below.
Sub myChart()
Dim myChart As Chart, cht As ChartObject
Dim rngChart As Range, destSht As String
Dim rngData As Range
destSht = ActiveSheet.Name
Set myChart = Charts.Add
Set myChart = myChart.Location(where:=xlLocationAsObject, Name:=destSht)
With ActiveSheet.ListObjects("Table1").DataBodyRange
Set rngData = Union(.Columns(2), .Columns(9), .Columns(10), .Columns(11), .Columns(12))
End With
With myChart
.SetSourceData Source:=rngData, PlotBy:=xlColumns
.ChartType = xlColumnClustered
.ChartStyle = 209
*****Below is the first series line that I want to format*****
With .FullSeriesCollection(2)
.ChartType = xlLine
.AxisGroup = 1
End With
With .FullSeriesCollection(3)
.ChartType = xlLineMarkers
.AxisGroup = 2
End With
*****2nd line I want to format*****
With .FullSeriesCollection(4)
.ChartType = xlLine
.AxisGroup = 2
End With
End With
ActiveSheet.ChartObjects(1).Activate
Set cht = ActiveChart.Parent
Set rngChart = Range("A1100:K1115")
cht.Left = rngChart.Left
cht.Top = rngChart.Top
cht.Width = rngChart.Width
cht.Height = rngChart.Height
Range("A2").Select
End Sub
With .FullSeriesCollection(2)
.ChartType = xlLine
.AxisGroup = 1
.Format.Line.Weight = 5
End With
Worked fine for me in Office 2016.
I would have commented, but I do not have a high enough reputation yet
I have to create almost 200 charts of time series. So I tried to write a macro that finishes most of the work I need to do.
I generated names for the time series like this as an example:
Name:= AKB_ExampleA
The name refers to a dynamic range which I declared with this formula:
=OFFSET('sheet1'!$C$7:$C$137;0;0;COUNT('sheet1'!$C$7:$C$206))
So now to the macro I coded so far:
Sub graphik_erstellen()
Call graphik1("AKB")
End Sub
Sub graphik(Name As String)
'
Dim Ch As Chart
Dim RngToCover As Range
Set Ch = charts.Add
Set Ch = Ch.Location(Where:=xlLocationAsObject, Name:="Charts")
With Ch
.ChartType = xlLine
.SetSourceData Source:=Range(Name & "_ExampleA")
.SeriesCollection(1).XValues = Range("Datum_Volumen")
.SeriesCollection(1).Name = "SERIES1"
.FullSeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(192, 0, 0)
.Transparency = 0
End With
.HasTitle = True
.ChartTitle.Text = Name & ", Volumen (nach Korrektur)"
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
.Legend.Select
Selection.Format.TextFrame2.TextRange.Font.Size = 11
Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue
With .Parent
.top = 100
.left = 100
.height = 287.149606299
.width = 543.685039370078
.Name = Name & "_chart"
End With
End With
End Sub
My problem is, that if I do that, the dynamic range is not really considered. It takes the range of the name (which is $C$7:$C$137) but it should refer to the name itself (in order to be dynamic).
So if I click on the chart to see the series, the series values are declared as: ='sheet1'!$C$7:$C$137 instead of ='sheet1'!ExampleA.
I would be really, really grateful if somebody could help me out.
Best
Elio
I have rearranged a few lines of code and tried to place comments refering to them as well.
Let me know what works. Youjust might need to change SeriesCollection to FullSeriesCollection. Other than that the code works in my Excel 2010.
The first Sub I just get the Range size according to the data available in Column "C" from Row 7.
Let me know.
Option Explicit
Sub graphik_erstellen()
'You always want to use direct reference to a sheet/range chart
'Refering to the WorkBook they are in and the worksheet as well.
'especially if you are opening multiple WorkBooks / Sheets
Dim CurrentWorkSheet As Worksheet
Set CurrentWorkSheet = Workbooks("Book1").Worksheets("Sheet1")
'Dynamically finding the end of the data in Column C
Dim LastRow As Long
LastRow = CurrentWorkSheet.Cells(CurrentWorkSheet.Rows.Count, "C").End(xlUp).Row
'Setting the range using the document reference aswell
Dim AKB As Range
Set AKB = Workbooks("Book1").Worksheets("Sheet1").Range(Cells(7, "C"), Cells(LastRow, "C"))
Call graphik(AKB)
End Sub
Sub graphik(Name As Range)
Dim DataChart As Chart
Dim RngToCover As Range
Set DataChart = Workbooks("Book1").Charts.Add
'With Excel 2010 the line above will automatically add the chart as a sheet and not aobject in a sheet
'Set DataChart = DataChart.Location(Where:=xlLocationAsObject, Name:="Charts")
With DataChart
.Name = "Charts" ' This will be the Name of the CHart Tab
.ChartType = xlLine
.SetSourceData Source:=Name
'You can see below I avoided the Select and Selection
With .SeriesCollection(1)
'Using Offset I just used the data one cell to the left of the range
.XValues = Name.Offset(0, -1)
.Name = "SERIES1"
With .Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(192, 0, 0)
.Transparency = 0
End With
End With
.HasTitle = True
.ChartTitle.Text = "MIDDEL TOP TEXT" 'Name & ", Volumen (nach Korrektur)"
.HasLegend = True
With .Legend
.Position = xlLegendPositionBottom
.Format.TextFrame2.TextRange.Font.Size = 11
.Format.TextFrame2.TextRange.Font.Bold = msoTrue
End With
'Not sure about this, it doesnt work in my Excel 2010
'
With .Parent
.Top = 100
.Left = 100
.Height = 287.149606299
.Width = 543.685039370078
.Name = Name & "_chart"
End With
End With
End Sub
Let me know what your intention is for the Sheet and Chart names and then I can help with getting that to what you need as well.