Re: Defining Variable Data Row For Chart Series
Hi Dave/Andy.
I meant it bugged out at the same point with regard to the first posting that andy supplied (see below) which he then incorporated into his further posting.
Quote from Andy Pope
try
[vba]ActiveChart.SeriesCollection(1).XValues = _
"='Chart Data'!R1C10:" & range("'Chart Data'!J1").End(xltoright).Address( , ,xlR1C1)[/vba]
Anyway, I have now tried Andy's code in my book1.xls and it works fine so It must be something happening due to the rest of my code.
I'm not sure if it is useful to include the full code of my macro (Hopefully i've done something obvious) but here goes...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
If Target.Column <> 7 Then Exit Sub
Cancel = True
Dim MyName As String
'Unhide worksheets
Data.Visible = True
Chart_Data.Visible = True
'Clear Column Headers and Clear Graph data on Chart Data Sheet
Chart_Data.Select
Chart_Data.Rows("1:2").Select
Selection.ClearContents
Chart_Data.Range("A1").Select
'Copy in Header row
Data.Select
Data.Rows("3:3").Copy
Chart_Data.Select
Chart_Data.Rows("1:1").Select
Chart_Data.Paste
'Copy in selected data
Convalue = Chart_Data.Cells(Target.Row, 4).Address
KPI = Range(Convalue).Value
Dim c, DataRow
With Data
Set c = .Range("A5:A350").Find(KPI, LookIn:=xlValues)
If Not c Is Nothing Then
DataRow = c.Row
End If
End With
If DataRow = "" Then
'Hide Worksheets
Data.Visible = False
Chart_Data.Visible = False
Rags.Select
MsgBox (" No Data found")
Else
Data.Select
Data.Rows(DataRow).Copy
Chart_Data.Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Chart_Data.Select
Chart_Data.Range("A1").Select
Chart_Data.Range("G2").Copy
Chart_Data.Rows("3:3").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
With Selection.Interior
.ColorIndex = 4
.Pattern = xlSolid
End With
Selection.Font.ColorIndex = 0
With Selection.Font
.Name = "Arial"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Chart_Data.Range("H2").Copy
Chart_Data.Rows("4:4").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
With Selection.Font
.Name = "Arial"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 50
End With
Selection.Font.ColorIndex = 0
With Selection.Interior
.ColorIndex = 3
.Pattern = xlSolid
End With
Chart_Data.Cells.EntireColumn.AutoFit
Chart_Data.Range("A1").Select
'Name Chart sheet to relevant Name (Myname)
Chart_Data.Select
Chart_Data.Range("G2").Select
MyName = Left(ActiveCell, 31) 'Can only be 30 Characters long
'Create Graph
Charts.Add
With ActiveChart
.ChartType = xlLine
' clear formats fails if chart has no default data
If .SeriesCollection.Count > 0 Then .PlotArea.ClearFormats
' remove any data plotted by active cell containing data
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
'Data Applied to Graph
With .SeriesCollection.NewSeries
.XValues = _
"='Chart Data'!R1C10:" & Range("'Chart Data'!J1").End(xlToRight).Address(, , xlR1C1)
' change R1 to R2 in order for address to be correct
.Values = _
"='Chart Data'!R2C10:" & Range("'Chart Data'!J2").End(xlToRight).Address(, , xlR1C1)
.Name = "='Chart Data'!R2C4"
End With
'Green Threshold Applied to Graph
If Chart_Data.Range("G2").Value > 0 Then
With .SeriesCollection.NewSeries
.Values = _
"='Chart Data'!R3C10:" & Range("'Chart Data'!J3").End(xlToRight).Address(, , xlR1C1)
.Name = "='Chart Data'!R3C8"
End With
End If
'Red Threshold Applied to Graph
If Chart_Data.Range("H2").Value > 0 Then
With .SeriesCollection.NewSeries
.Values = _
"='Chart Data'!R4C10:" & Range("'Chart Data'!J4").End(xlToRight).Address(, , xlR1C1)
.Name = "='Chart Data'!R4C8"
End With
End If
End With
'Format Data Line
ActiveChart.SeriesCollection(1).Select
With Selection.Border
.Weight = xlMedium
.LineStyle = xlContinuous
End With
'Green Threshold Applied to Graph
ActiveChart.SeriesCollection(2).Select
With Selection.Border
.ColorIndex = 4
.Weight = xlThick
.LineStyle = xlContinuous
End With
With Selection
.MarkerBackgroundColorIndex = xlNone
.MarkerForegroundColorIndex = xlNone
.MarkerStyle = xlNone
.Smooth = False
.MarkerSize = 3
.Shadow = False
End With
'Red Threshold Applied to Graph
ActiveChart.SeriesCollection(3).Select
With Selection.Border
.ColorIndex = 3
.Weight = xlThick
.LineStyle = xlContinuous
End With
With Selection
.MarkerBackgroundColorIndex = xlNone
.MarkerForegroundColorIndex = xlNone
.MarkerStyle = xlNone
.Smooth = False
.MarkerSize = 3
.Shadow = False
End With
ActiveChart.Location Where:=xlLocationAsNewSheet
'ActiveChart.Location Where:=xlLocationAsObject, Name:="Q4" 'If you want graph on sheet
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = Chart_Data.Range("E2") + " - " + Chart_Data.Range("G2")
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
'Adds "Close Graph" Box and assigns "Close_graph" Macro to Box
ActiveChart.Shapes.AddTextbox(msoTextOrientationHorizontal, 568.52, 52.02, _
97.17, 36.67).Select
Selection.Characters.Text = "Close Graph" & Chr(10) & "Click HERE"
Selection.AutoScaleFont = False
With Selection.Characters(Start:=1, Length:=18).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Selection.AutoScaleFont = False
With Selection.Characters(Start:=19, Length:=4).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleSingle
.ColorIndex = xlAutomatic
End With
Selection.ShapeRange.ScaleWidth 0.76, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 51
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 51
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.OnAction = "Close_Graph"
ActiveChart.ChartArea.Select
'Name Chart sheet to relevant Name (Myname) and Changes X-axis format
ActiveChart.Select
ActiveChart.Name = MyName
With ActiveChart.Axes(xlCategory)
.AxisBetweenCategories = False
.TickLabels.NumberFormat = "mmm-yy"
End With
'Hide Worksheets
Data.Visible = False
Chart_Data.Visible = False
Calculate
End If
Application.ScreenUpdating = True
End Sub
Display More