VBA在新工作表中显示图表,如果已经存在则将其删除

我想在一个新的工作表中显示一个图表,如果已经存在,请将其删除。我很确定(可能只有一点),我确定代码正确,但是感觉布局有问题或我完全丢失了。这是我的代码和正在使用的电子表格的图片:

Sub HistoricalDemand()
For Each ws In Worksheets
    If ws.Name = "Demand Line Chart" Then
        Application.DisplayAlerts = False
        Sheets("Demand Line Chart").Delete
        Application.DisplayAlerts = True
    End
End If
Next

ActiveChart.ChartArea.Select
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Demand Line Chart"

Columns("A:A").Select
Selection.NumberFormat = "[$-en-US]mmm-yy;@"

Range("A:A,E:E").Select
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
' Try With command here
ActiveChart.SetSourceData Source:=Range("A:A,E:E")
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.Orientation = 70
Selection.MajorTickMark = xlNone
With ActiveChart
    .Axes(xlCategory).Select
    .Axes(xlCategory).MajorUnit = 2
    .ChartTitle.Select
    .ChartTitle.Text = "Historical Demand"
    .SetElement (msoElementLegendRight)
Selection.Format.TextFrame2.TextRange.Characters.Text = "Historical Demand"
With Selection.Format.TextFrame2.TextRange.Characters(1, 17).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
ActiveChart.ChartArea.Select
End With
End Sub

enter image description here

评论