Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if chart was already created

Tags:

excel

vba

I'm adding a chart via vba with the following commands:

Dim CsvVisualization As ChartObject
Set ChartSizePosition = Range("B8:I25")
Set CsvVisualization = ThisWorkbook.Sheets("Übersicht").ChartObjects.Add(ChartSizePosition.Left, ChartSizePosition.Top, ChartSizePosition.Width, ChartSizePosition.Height)
CsvVisualization.Name = "MeasurementVisualization"
With CsvVisualization.Chart
    .ChartType = xlXYScatterSmoothNoMarkers
...

How could I proofe if this chart was already created when the excel file is reopened? I tried something like:

If CsvVisualization Is Nothing Then

But CsvVisualization won't hold it's value when the file is closed and opened again.

Thank you for help!

like image 243
user3470049 Avatar asked Jan 08 '23 11:01

user3470049


1 Answers

Dim CsvVisualization As ChartObject
Dim MyChartName as string
Dim CreateChart as Boolean

MyChartName = "MeasurementVisualization"
CreateChart = True

If ThisWorkbook.Sheets("Übersicht").ChartObjects.Count = 0 Then
  For Each CsvVisualization in ThisWorkbook.Sheets("Übersicht").ChartObjects
    If CsvVisualization.name = MyChartName then
      CreateChart = False
    End If
  Next
End if

If CreateChart then
  Set ChartSizePosition = Range("B8:I25")
  Set CsvVisualization = ThisWorkbook.Sheets("Übersicht").ChartObjects.Add(ChartSizePosition.Left, ChartSizePosition.Top, ChartSizePosition.Width, ChartSizePosition.Height)
CsvVisualization.Name = "MeasurementVisualization"
  With CsvVisualization.Chart
    .ChartType = xlXYScatterSmoothNoMarkers
...
End If
like image 59
FreeMan Avatar answered Jan 14 '23 22:01

FreeMan