Hey guys, i am very new to VBA . I have created 2 buttons (one to generate ECG graph and another one to generate Heartrate graph).
The codes works when i test them individually , but it cant display both graphs on the same worksheet (Eg: When i click ECG's button, ECG graph generated but i MUST delete that ECG graph first before clicking the heartrate button, otherwise if i did not delete, the code will automatically delete my 1st graph, shifting my components and a runtime error - 1004: "This object doesnt have a legend " will appear.
However if i delete the first graph generated before clicking the 2nd button, the 2nd graph will be generated successfully. :(

Here are the VBA codes for my 2 buttons:

ECG graph Button code:
Sub ECG()
Dim myChartecg As Chart, chtecg As ChartObject
Dim rngChartecg As Range, destinationSheet As String

Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$50000").AutoFilter Field:=1, Criteria1:="<10", _
Operator:=xlAnd, Criteria2:="<20"
Range("A2:A50000").Select
Selection.Copy
Range("V2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFilter

'Destination of the chart to appear'
destinationSheet = ActiveSheet.Name
'Create a chart'
Set myChartecg = Charts.Add
Set myChartecg = myChartecg.Location(xlLocationAsObject, Name:=destinationSheet)
myChartecg.SetSourceData Source:=Range("V2:V50000").CurrentRegion, PlotBy:=xlColumns
'Type of chart you want'
myChartecg.ChartType = xlXYScatterLinesNoMarkers
Set chtecg = ActiveChart.Parent
ActiveChart.Parent.Name = "ECGChart"
ActiveSheet.ChartObjects("ECGChart").Activate

'to set Max X axis scale'
a = Worksheets("Sheet1").Cells(Rows.Count, "V").End(xlUp).Row
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MaximumScale = a
ActiveChart.FullSeriesCollection(1).Name = "=""ECG Waveform"""
ActiveChart.FullSeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(102, 153, 255)
.Transparency = 0
End With

'Specify the size of chart'
Set rngChartecg = Range("D7: P20")
'specify the position'
chtecg.Left = rngChartecg.Left
chtecg.Top = rngChartecg.Top
chtecg.Width = rngChartecg.Width
chtecg.Height = rngChartecg.Height
'Delete legend'
ActiveChart.Legend.Select
Selection.Delete
ActiveWindow.Visible = False
Range("V2").Select
End Sub



HeartRate button code:
Sub HeartRate()
Dim myChart As Chart, cht As ChartObject
Dim rngChart As Range, destinationSheet As String

Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$50000").AutoFilter Field:=1, Criteria1:=">10", _
Operator:=xlAnd, Criteria2:=">50"
Range("A2:A50000").Select
Selection.Copy
Range("Y2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFilter

'Destination of the chart to appear'
destinationSheet = ActiveSheet.Name
'Create a chart'
Set myChart = Charts.Add
Set myChart = myChart.Location(xlLocationAsObject, Name:=destinationSheet)
myChart.SetSourceData Source:=Range("Y2:Y50000").CurrentRegion, PlotBy:=xlColumns

'Type of chart you want'
myChart.ChartType = xlXYScatterLinesNoMarkers

ActiveSheet.ChartObjects(1).Activate
Set cht = ActiveChart.Parent

'to set Max X axis scale'
a = Worksheets("Sheet1").Cells(Rows.Count, "Y").End(xlUp).Row
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MaximumScale = a
ActiveChart.FullSeriesCollection(1).Name = "=""HeartRate"""
ActiveChart.FullSeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 124, 128)
.Transparency = 0
End With

'Specify the size of chart'
Set rngChart = Range("D24: K33")
'specify the position'
cht.Left = rngChart.Left
cht.Top = rngChart.Top
cht.Width = rngChart.Width
cht.Height = rngChart.Height
'Delete legend'
ActiveChart.Legend.Select
Selection.Delete

'Generate heartrate calculation'
Range("O28").Select
ActiveCell.FormulaR1C1 = "=MAX(C[10])"
Range("O30").Select
ActiveCell.FormulaR1C1 = "=MIN(C[10])"
Range("O32").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(C[10])"
Range("O32").Select
Selection.NumberFormat = "0"


Range("Y2").Select
End Sub

I tried googling so much but other online codes doesn't work.. Please help me thank you, i really need the two buttons to generate both graphs on the same excel worksheet :( !!!!! Thanks again