title: Why does Chart.SetSourceData Source:=Nothing crashes Excel?
body: <p><a href="https://docs.microsoft.com/en-us/office/vba/api/excel.chart.setsourcedata" rel="nofollow noreferrer">Chart.SetSourceData</a> expects a Range as Source.</p>
  
  <p>If there's a bug in the code and the variable that holds the Range is set to the value <code>Nothing</code> at some point it will either throw an error or crash Excel.</p>
  
  <p>My question is, in the following code, why does using <code>Object</code> instead of a <a href="https://docs.microsoft.com/en-us/office/vba/api/excel.chart(object)" rel="nofollow noreferrer"><code>Chart</code></a> object has different behavior and why does it crash Excel?</p>
  
  <pre><code>Private Sub Test()
  
    Dim Workbook As Excel.Workbook
    Dim Worksheet As Excel.Worksheet
    Dim Shapes As Excel.Shapes
    Dim Shape As Excel.Shape
  
    Dim Object As Object
    Dim Chart As Excel.Chart
  
    Set Workbook = Excel.ThisWorkbook
    Set Worksheet = Workbook.ActiveSheet
    Set Shapes = Worksheet.Shapes
    Set Shape = Shapes.AddChart
  
    Set Object = Shape.Chart
    On Error Resume Next
    Object.SetSourceData Source:=Nothing ' Throws run-time error
    On Error GoTo 0
  
    Set Chart = Shape.Chart
    Chart.SetSourceData Source:=Nothing ' Crashes Excel regardless of using error handling or not
  
  End Sub
  </code></pre>