I have an excel workbook with multiple sheets. The first sheet is a dashboard on which I display charts based on data from other sheets. I have a sheet called Daily Score. In it are three columns: User ID, Sum_PointsAdded, Day_Timestamp On any given day, a user can have a row inserted with the points they added and the date added. The header row starts on A3 and has auto filter applied.
On the dashboard I have created a button that asks the user to insert a UserID via an input box. Based on that data, I filter the data to show only that user's score. A sample result would be:
User ID Sum_PointsAdded Day_Timestamp
777 38 28/3/19
777 11 20/3/19
777 44 2/4/19
777 24 13/5/19
The chart I add is a line chart and should show only one line (and one entry type in the legend). The X axis is the date and the Y axis is the score.
If I manually use the filter on the Daily Score sheet, and then I click the button on the Dashboard sheet and request the filter, the chart appears OK. However, if I simply click the chart button on the dashboard and don't fiddle with the filter, the chart I get has a line for Sum_PointsAdded and a line for Day_Timestamp (that I shouldn't get) and no line for score. The legend has four entries: Score, Sum_PointsAdded, Day_Timestamp and Series4
I'm assuming I'm doing something wrong with the filter or the definitions of the range for the chart. However I do have another chart on the dashboard sheet with the exact same set up (just with two lines on the chart), and there I have no problem. I've tried changing the location of the range definitions but it doesn't seems to work. Note: these are dynamic ranges. Here's my code:
Public Sub CreateDailyScoreChart()
Dim WS As Worksheet
Dim WS2 As Worksheet
Dim Rng1 As Range
Dim Rng2 As Range
Dim myValue As Variant
Dim LastRow As Long
Dim FirstRow As Long
Application.ScreenUpdating = False
Set WS = ThisWorkbook.Sheets("Daily Score")
Set WS2 = ThisWorkbook.Sheets("Dashboard")
With WS
'set last row for entire table in order to define range
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
Set Rng1 = .Range("A3:A" & LastRow)
Set Rng2 = .Range("C3:C" & LastRow)
End With
'Input UserID
myValue = InputBox("Insert UserID")
'Filter based on UserID, for this month decending
Sheets("Daily Score").Activate
On Error Resume Next
ActiveSheet.ShowAllData
Rng1.CurrentRegion.AutoFilter Field:=1, Criteria1:="=" & myValue
On Error GoTo 0
With WS.AutoFilter.Sort
.SortFields.Clear
.SortFields.Add2 Key _
:=Rng2, SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Rng2.AutoFilter Field:=3, Criteria1:=13, _
Operator:=11, Criteria2:=0, SubField:=0
With WS
FirstRow = .AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Row
'reset last row for filtered table
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
End With
'Delete existing chart if it exists
On Error Resume Next
With WS2
.ChartObjects("DailyScore").Activate
.ChartObjects("DailyScore").Delete
End With
On Error GoTo 0
'Add new chart
'ThisWorkbook.Sheets("Dashboard").Shapes.AddChart2(322, xlLineMarkers).Select
WS.Activate
WS.Shapes.AddChart2(332, xlLineMarkers).Select
Dim Chart As Chart
Set Chart = ActiveChart
'Defining X and Y Axis values
Dim xRng As Range
Dim vRng1 As Range
With WS
Set xRng = .Range(.Cells(FirstRow, 3), .Cells(LastRow, 3))
Set vRng1 = .Range(.Cells(FirstRow, 2), .Cells(LastRow, 2))
End With
'Adding series 1
Chart.SeriesCollection.NewSeries
Chart.FullSeriesCollection(1).XValues = xRng
Chart.FullSeriesCollection(1).Values = vRng1
Chart.FullSeriesCollection(1).Name = "Score"
Chart.SetElement (msoElementLegendBottom)
Chart.SetElement (msoElementChartTitleAboveChart)
Selection.Caption = "User " & myValue & " Daily Score This Year"
Chart.Parent.Name = "DailyScore"
Chart.ChartArea.Select
Chart.Parent.Cut
Sheets("Dashboard").Select
Sheets("Dashboard").Activate
Range("K20").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End Sub
F8
.. That will execute your code line by line. That will help you identify what is going on Each step. – MikkuSet Chart = ActiveChart
and not created empty and then values added, and so things get screwed up. How do I get the graph to be created empty? – BuddyF8
idea, and pinpointing the problem to the fact that the chart was created with data that was throwing it off, I simply addedActiveChart.ChartArea.ClearContents
after theSet Chart = ActiveChart
and it solved the problem. Chart content was emptied and chart came back with desired result. Thanks for the help – Buddy