title: VBA If <object> Is Nothing
body: <p>I want to test an object to see if it doesn't exist. If it does not exist, I just want to have a MsgBox show up (or write Error in cell A1 or something). Banana does not exist in this XML. </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;catalog&gt;
  &lt;book id="Adventure"&gt;
     &lt;author&gt;Gambardella, Matthew&lt;/author&gt;
     &lt;title&gt;XML Developer's Guide&lt;/title&gt;
     &lt;price&gt;44.95&lt;/price&gt;
  &lt;/book&gt;
  &lt;book id="Adventure"&gt;
     &lt;author&gt;Ralls, Kim&lt;/author&gt;
     &lt;title&gt;Midnight Rain&lt;/title&gt;
     &lt;price&gt;5.95&lt;/price&gt;
  &lt;/book&gt;
  &lt;book id="Adventure"&gt;
     &lt;author&gt;Boal, John&lt;/author&gt;
     &lt;title&gt;Mist&lt;/title&gt;
     &lt;price&gt;15.95&lt;/price&gt;
  &lt;/book&gt;
  &lt;book id="Mystery"&gt;
     &lt;author&gt;Ralls, Kim&lt;/author&gt;
     &lt;title&gt;Some Mystery Book&lt;/title&gt;
     &lt;price&gt;9.95&lt;/price&gt;
  &lt;/book&gt;
  &lt;/catalog&gt;
  </code></pre>
  
  <p>The test code:</p>
  
  <pre><code>Sub mySub()
  
  Dim XMLFile As Variant
  Dim Author As Object
  Dim athr As String, BookType As String, Title As String, StoreLocation As String
  Dim AuthorArray() As String, BookTypeArray() As String, TitleArray() As String, StoreLocationArray() As String
  Dim i As Long, x As Long, j As Long, pn As Object, loc As Object, arr, ln As String, loc2 As Object
  
  Dim mainWorkBook As Workbook
  Dim n As IXMLDOMNode
  Set mainWorkBook = ActiveWorkbook
  Set XMLFile = CreateObject("Microsoft.XMLDOM")
  XMLFile.Load ("C:\BooksOriginal.xml")
  
  x = 1
  j = 0
  
  
  Set Author = XMLFile.SelectNodes("/catalog/book/banana")
  
  If Author Is Nothing Then
      MsgBox ("Not Found")
      Range("A1").Value = "Not found"
  End If
  
  If Not Author Is Nothing Then
      For i = 0 To (Author.Length - 1)
  
      athr = Author(i).Text
  
      If athr = "Ralls, Kim" Then
  
          Set pn = Author(i).ParentNode
          BookType = pn.getAttribute("id")
          Title = pn.getElementsByTagName("title").Item(0).nodeTypedValue
  
          AddValue AuthorArray, athr
          AddValue BookTypeArray, BookType
          AddValue TitleArray, Title
          AddValue StoreLocationArray, StoreLocation
  
          j = j + 1
          x = x + 1
      End If
  Next
      Range("A3").Resize(j, 1).Value = WorksheetFunction.Transpose(AuthorArray)
      Range("B3").Resize(j, 1).Value = WorksheetFunction.Transpose(BookTypeArray)
  End If
  
  End Sub
  
  'Utility method - resize an array as needed, and add a new value
  Sub AddValue(arr, v)
      Dim i As Long
      i = -1
      On Error Resume Next
      i = UBound(arr) + 1
      On Error GoTo 0
      If i = -1 Then i = 0
      ReDim Preserve arr(0 To i)
      arr(i) = v
  End Sub
  </code></pre>
  
  <p>Why does this block not do anything? I feel like it's being completely overlooked at by VBA. I even tried putting an End in the If statement.</p>
  
  <pre><code>If Author Is Nothing Then
      MsgBox ("Not Found")
      Range("A1").Value = "Not found"
      End
  End If
  </code></pre>
  
  <p>Also, the error is also thrown at the print range line.. which is in the If Not Author Is Nothing statement. Very strange.</p>